Join | Login | Why Join?   
SQL Server, Oracle, DB2, Sybase, MySQL Help - SSWUG.ORG HACKER SAFE certified sites prevent over 99.9% of hacker crime.
Search SSWUG:   
 
Access to 467 free guest articles, discussions and more, just create your free SSWUG User ID:
Email address:  
This will be your login ID - we'll email you your password - you'll even receive the newsletter, opt-out at any time.
Email to Friend //  Discuss Article //  Rate Article //  Digg Article //  Add to Del.icio.us //  Add to Technorati

Full site membership is required for this article. Already a full member? Login here.
You currently have a guest membership. You can upgrade to full site membership here (free trial available).

Table Variables are Faster Than Temp Tables - Fact or Fiction


 Article Abstract:

(Ben Taylor) I have read in numerous articles and postings that table variables are a preferred method for temporary storage in stored procedures. They state that the use of temporary tables in tempdb can not perform as quickly as a table variable in memory. This has been such a general statement without qualification that I feel I must contest. In fact, I have stored procedures that prove contrary. This article reviews one condition that I find common where a temporary table out performs a table variable 1,000%.

 Read this article...

 Related Articles - For Members.
All Articles By Author

Second Normal Form (2NF)
SQL Server 6.5: Some useful undocumented DBCC commands
SQL Server 2000 useful undocumented stored procedures
Optimizing Stored Procedures To Avoid Recompiles
Stored Procedures Administration
Tips for using SQL Server 2005 stored procedures
Meta Data User-Defined Functions
Some tips about tempdb database
Tempdb database optimization tips
Automate Database Defragmentation and Statistics Updates



Key (Please note):
(R) - registration may be required for access at the target site
($) - target site may require paid membership for access to this or other content


Reader Comments: Post Your Comments/Feedback

RE: Table Variables are Faster Than Temp Tables - Fact or Fiction
(posted: 6/28/2005 11:57:25 AM)
> Always test your stored procedures for performance

Indeed, if in doubt test it out. As mentioned on various occasions, performance is largely a physical property that may be expected to vary with resources and utilization. HTH



RE: Table Variables are Faster Than Temp Tables - Fact or Fiction
(posted: 6/28/2005 12:26:46 PM)
declare @Filter TABLE (
CUST_ID INT NOT NULL,
INV_ID INT NOT NULL,
INV_START SMALLDATETIME NOT NULL,
INV_END SMALLDATETIME NOT NULL
unique clustered cust_id, inv_id)

This technique was covered beautifully by Tom Moreau in an article titled "How Table Variables Can Speed Up Your Queries".

ffoiii
Frank F. Owen III




RE: Table Variables are Faster Than Temp Tables - Fact or Fiction
(posted: 6/28/2005 12:39:23 PM)
There is a major flaw in your testing method. One reason that it often favorable to use table variables is that it doesn't cause frequent recompilation of procedures like temp tables do. By clearing the proc cache for each run, you've totally illiminated that factor from the testing. That would be like tying Michael Johnson's ankles together and then bragging because you beat him in a foot race.

Nonetheless, I agree that using table variables instead of temp tables is not always better.



RE: Table Variables are Faster Than Temp Tables - Fact or Fiction
(posted: 7/5/2005 12:12:16 AM)
Thanks for the comments. I took them seriously and tested using each of your recommendations. Here are the results:

Changed temp tables to temp memory variables declared with unique clustered. Ran multiple tests and did not clear any cache.

Temp tables performed an average of 4 times faster than table variables.

Cheers,

BST



Email to Friend //  Discuss Article //  Rate Article //  Digg Article //  Add to Del.icio.us //  Add to Technorati

   




 

[ Register ] [ Webcasts ] [ Podcasts ] [ Newsletter Archive ] [ RSS/Feeds ]
[ About ] [ Advertise ] [ Contact ] [ Privacy ] [ Terms of Service ]
[ Link to SSWUG ] [ List Server Archives ] [ Recent Orig. Content ]
(c) 1997-2008, Bits on the Wire, Inc.  (0)

Some names and products covered by SSWUG are the registered trademarks of their respective owners.
DAA10354WWW004