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
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
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.
精彩评论