SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Table variable vs temp table


Table variable vs temp table

Author
Message
dgvozdetsky
dgvozdetsky
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2396 Visits: 113
Very interesting question :-)
In my case on SQL 2008 R2 i receive follow result:
- table variable - 29290 ms
- temp table - 240 ms
tommyh
tommyh
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2536 Visits: 2000
dgvozdetsky (10/3/2011)
Very interesting question :-)
In my case on SQL 2008 R2 i receive follow result:
- table variable - 29290 ms
- temp table - 240 ms


Cool. The comment block in the start of the question have both 2005 and 2008 reference so didnt really know if that would effect anything. And since i dont have a 2008 (let alone an R2). Hard to test.

I gave it a try on 2000 and that was faster for me (like 70ms +/- 10ms). Strange effect on 2008 i must say.

/T
abhiraami.kasilakshmi
abhiraami.kasilakshmi
SSC Rookie
SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)

Group: General Forum Members
Points: 25 Visits: 3
The site which i referred regarding this says that table variables are stored only in memory. so the access time for a table variable can be faster than the time it takes to access a temporary table.

Note: http://searchsqlserver.techtarget.com/tip/Temporary-tables-in-SQL-Server-vs-table-variables

but when i tried to execute the query it results both as same....

So which one is the correct answer...
dgvozdetsky
dgvozdetsky
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2396 Visits: 113
tommyh (10/3/2011) Strange effect on 2008 i must say.

/T

As known, Microsoft was changed work with tempDB since SQL 2005, so it is possible, that time dimensions in SQL 2000 differ with time dimensions in SQL 2k5, 2k8, 2k11 etc.

Excuse my bad english :-)
Britt Cluff
Britt Cluff
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2371 Visits: 253
Missed it. Thanks for the question.

http://brittcluff.blogspot.com/
Thomas Abraham
Thomas Abraham
Hall of Fame
Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)

Group: General Forum Members
Points: 3823 Visits: 2256
I got it wrong, so I tested it on my own system. (2008 R2 - 2005 not available)

Results for table variable, in ms: 80800, 80203, & 79523

Results for temp table, in ms: 463, 450, 470

So, now I'm a bit confused. Just when am I supposed to use table variables??

Please don't go. The drones need you. They look up to you.
Connect to me on LinkedIn
Thomas Abraham
Thomas Abraham
Hall of Fame
Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)

Group: General Forum Members
Points: 3823 Visits: 2256
Oops, where are my manners. Thank you sir, may I please have another? (Question, that is.)

Please don't go. The drones need you. They look up to you.
Connect to me on LinkedIn
tabinsc
tabinsc
SSC Eights!
SSC Eights! (960 reputation)SSC Eights! (960 reputation)SSC Eights! (960 reputation)SSC Eights! (960 reputation)SSC Eights! (960 reputation)SSC Eights! (960 reputation)SSC Eights! (960 reputation)SSC Eights! (960 reputation)

Group: General Forum Members
Points: 960 Visits: 919
The Inserts both execute at about the same speed. It is the "Select where not in" statement that is considerably slower with the table variable. So I think the point of the question was not how fast the insert runs but how fast the select runs.

Tony
------------------------------------
Are you suggesting coconuts migrate?
Ninja's_RGR'us
Ninja's_RGR'us
SSC Guru
SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)

Group: General Forum Members
Points: 67117 Visits: 9671
You guys really need to learn english. There was rarely, if ever, a better written question.


It clearly stated to execute 3 batches separately with begin / end points in the code.



I just would like to add to the short explaination that the recompile speeds up the query because the row estimation is better which gives a different & better plan to execute.

Table variables estimate to 1 row which is way wrong in this case and causes the "bad" plan.
bitbucket-25253
bitbucket-25253
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15659 Visits: 25280
yogesh.balasubramanian (10/3/2011)
I see a relavent article and it seems like table variable is much faster than temp table.
Ref: http://sqlserverperformance.idera.com/uncategorized/performance-comparison-temp-tables-table-variables-sql-server/

Unsure Confused on which one is better?


One item that the question hoped to illustrate is that like most things in SQL SERVER (T-SQL) "IT DEPENDS"

For example - additional testing using SQL Server 2008
                     Processing Time (in milliseconds)
#Rows Table Variable Temp Table
20,000 68,773 386
2,000 633 70
200 33 23
20 16 20



Add in further modifications to the Temp Table, such as an index, and the results change again. So in the real world, do not depend upon
assumptions, or what others say is the best method. Test YOUR solution and test it again and again to insure it is the better solution before placing it in a production DB

If everything seems to be going well, you have obviously overlooked something.

Ron

Please help us, help you -before posting a question please read

Before posting a performance problem please read
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search