SQL Server 表变量与临时表
我正在使用表变量来存储查询的聚合结果。
查询如下
update @results
set col1 = totals
from ( select sum(x) as totals from ......)
where id = 1
如果我使用临时表,查询运行得更快。
在上面的查询中使用表变量还是临时表应该重要吗?
我是否忽略了什么?
谢谢
Possible Duplicate:
What's the difference between a temp table and table variable in SQL Server?
I am using a table variable to store the aggregate results of a query.
The query is as below
update @results
set col1 = totals
from ( select sum(x) as totals from ......)
where id = 1
If I use a temp table the query runs much faster.
Should the use of table variable or temp table matter in the query above?
Am I overlooking something?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这实际上取决于记录的数量。表变量在较小的记录集上表现更好。这是一篇很好的博客文章,其中包含一些基准测试: http:// /sqlnerd.blogspot.com/2005/09/temp-tables-vs-table-variables.html
It really depends on the amount of records. Table variables perform much better on smaller records sets. Here is a good blog post with some benchmarking: http://sqlnerd.blogspot.com/2005/09/temp-tables-vs-table-variables.html
表变量没有问题,直到您获得超过 100 个结果。如果您期望 100 个或更多结果,那么您应该切换到临时表以提高效率。
table variables are fine until you get more than 100 results. If you are expecting 100 results or more then you should switch to a temp table for efficiency.