Sur*_*har 5 sql-server temp-tables
我有一个疑问为什么我们应该使用临时表在临时表中有什么特殊的东西我们应该在哪里使用临时表.你能解释一下我或任何参考谢谢你.
Dr.*_*len 11
在编写T-SQL代码时,通常需要一个表来在执行该代码时临时存储数据.您有四个表选项:普通表,本地临时表,全局临时表和表变量.四个表选项中的每一个都有自己的用途和用途,每个选项都有其优点和问题:
* Normal tables are exactly that, physical tables defined in your database.
* Local temporary tables are temporary tables that are available only to the session that created them. These tables are automatically destroyed at the termination of the procedure or session that created them.
* Global temporary tables are temporary tables that are available to all sessions and all users. They are dropped automatically when the last session using the temporary table has completed. Both local temporary tables and global temporary tables are physical tables created within the tempdb database.
* Table variables are stored within memory but are laid out like a table. Table variables are partially stored on disk and partially stored in memory. It's a common misconception that table variables are stored only in memory. Because they are partially stored in memory, the access time for a table variable can be faster than the time it takes to access a temporary table.
Run Code Online (Sandbox Code Playgroud)
使用哪一个:
* If you have less than 100 rows generally use a table variable. Otherwise use a temporary table. This is because SQL Server won't create statistics on table variables.
* If you need to create indexes on it then you must use a temporary table.
* When using temporary tables always create them and create any indexes and then use them. This will help reduce recompilations. The impact of this is reduced starting in SQL Server 2005 but it's still a good idea.
Run Code Online (Sandbox Code Playgroud)
请参阅此页 http://www.sqlteam.com/article/temporary-tables
临时表有很多用途。它们在处理复杂查询中的数据时非常有用。您的问题很模糊,并且没有真正的答案,但我链接到一些临时表文档。
它们具有与表大部分相同的功能,包括约束和索引。它们可以是全局的或仅限于当前范围。它们也可能效率低下,因此请一如既往地保持谨慎。
http://www.sqlservercentral.com/articles/T-SQL/temptablesinsqlserver/1279/
http://msdn.microsoft.com/en-us/library/aa258255%28SQL.80%29.aspx
| 归档时间: |
|
| 查看次数: |
4316 次 |
| 最近记录: |