Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server 2000 temp table vs table variable

What would be more efficient in storing some temp data (50k rows in one and 50k in another) to perform come calculation. I'll be doing this process once, nightly.

How do you check the efficiency when comparing something like this?

like image 338
Saif Khan Avatar asked Sep 15 '26 08:09

Saif Khan


2 Answers

The results will vary on which will be easier to store the data, in disk (#temp) or in memory (@temp).

A few excerpts from the references below

  • A temporary table is created and populated on disk, in the system database tempdb.
  • A table variable is created in memory, and so performs slightly better than #temp tables (also because there is even less locking and logging in a table variable). A table variable might still perform I/O to tempdb (which is where the performance issues of #temp tables make themselves apparent), though the documentation is not very explicit about this.
  • Table variables result in fewer recompilations of a stored procedure as compared to temporary tables.
  • [Y]ou can create indexes on the temporary table to increase query performance.

Regarding your specific case with 50k rows:

As your data size gets larger, and/or the repeated use of the temporary data increases, you will find that the use of #temp tables makes more sense

References:

  • Should I use a #temp table or a @table variable?
  • MSKB 305977 - SQL Server 2000 - Table Variables
like image 91
boflynn Avatar answered Sep 17 '26 23:09

boflynn


There can be a big performance difference between using table variables and temporary tables. In most cases, temporary tables are faster than table variables. I took the following tip from the private SQL Server MVP newsgroup and received permission from Microsoft to share it with you. One MVP noticed that although queries using table variables didn't generate parallel query plans on a large SMP box, similar queries using temporary tables (local or global) and running under the same circumstances did generate parallel plans.

More from SQL Mag (subscription required unfortunately, I'll try and find more resources momentarily)

EDIT: Here is some more in depth information from CodeProject

like image 22
TheTXI Avatar answered Sep 17 '26 23:09

TheTXI



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!