0

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

Community
  • 1
  • 1
user55474
  • 537
  • 1
  • 8
  • 25
  • i am not sure this is an exact duplicate of the question mentioned. the post given in the link mentions the perf differences with respect to record counts. However in my case that record count does not appear to be the factor as there is only one record being updated – user55474 May 10 '11 at 02:36

2 Answers2

1

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

Dustin Laine
  • 37,935
  • 10
  • 86
  • 125
0

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.

Alexander Kahoun
  • 2,458
  • 24
  • 36
  • 2
    Why?? If a temp variable gets "too big", it will be stored into `tempDB` - just like a temp table. No real big difference here... – marc_s May 09 '11 at 18:51
  • That's deterministic of the environment, if memory is available then SQL Server will move the table back into memory. If you're constantly getting a large result it'll chew up the memory before storing it in the tempdb which does have a performance cost. This can be avoided all together by using a temp table to start with for large results. 100 results is just a rule-of-thumb, not a hard requirement. – Alexander Kahoun May 09 '11 at 19:19