June 4, 2004 at 12:21 am
June 4, 2004 at 1:24 am
From my own experience, if you have very large tables then rather than joining them together for a query you would do use temporary tables to store the results of your selects and then join the much smaller resulting temp tables.
If you have a medium sized database then I would prefer derived queries.
Table variables seem to get stored in memory, where are temporary tables get stored on the disk within TEMPDB.
It is worth comparing the execution plans between temp tables and table variables when carrying out SELECT, INSERT, DELETE , UPDATE actions. I seem to remember that INSERTs and SELECT are very fast on table variables but UPDATEs and DELETEs were more expensive. Don't quote me on that.
Another factor is the structure of the table. If it is very simple then I would lean towards table variables and conversely, if it was a larger structure I would lean towards temporary tables.
There isn't really a hard and fast rule.
June 4, 2004 at 5:40 am
I have ran into this debate as well. In my experience (as well as David's) the only way to get to which is better is to build it and see.
Table variables I have used to hold very small static data for "lookup" only. I have not had good luck with them other than that.
Temp tables I use extensively to pass data back and forth when indexes are required and/or a lot of crunching
Derived tables I also use to help limit the amount of data I am processing.
I prefer the last two options. I have heard that table variables load into memory but honestly again haven't had much luck. Hopefully in the next release of SQL they will work better and become a better tool.
Good Luck,
Good Hunting!
AJ Ahrens
webmaster@kritter.net
June 17, 2004 at 2:08 pm
I agree, I think looking at the overall purpose of the query, execution plan and IO statistics will truly tell what method is optimal. Each method is good depends on your purpose.
June 17, 2004 at 9:01 pm
SQL Server maintains statistics for queries that use temporary tables but not for queries that use table variables. Without statistics, SQL Server might choose a poor processing plan for a query that contains a table variable. It also prevents the engine to run the query in parallel when table variable is in the query.
See KB 305977 for more information.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply