Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

@Table vs #Table Expand / Collapse
Author
Message
Posted Thursday, March 21, 2013 9:45 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, February 28, 2014 8:28 AM
Points: 29, Visits: 21
I'll start by saying that I am a C# developer, not a DBA. When it comes to performance tweaks and making the Procs I write run the fastest they can, I trust the DBAs.

My last job, I was told by more that one DBA that table variables run in RAM and do not access the hard drive. I worked with the DBAs for over six years and came to trust & value what they told me. I used Table Variables when ever possible and didn't use Temp Tables.

While reworking a (messy) stored procedure at my current job the DBA told me "That is a common misconception. Table Variables and Temp Tables both access TempDB. Therefore, IO hard drive access."

Which DBA is correct?
Post #1433875
Posted Thursday, March 21, 2013 9:51 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 11:09 AM
Points: 20,806, Visits: 32,738
The one you work with now. However, it is possible that one or both will access tempdb or that one or both will reside completely in memory. It all depends on the amount of data and the query.

The biggest thing to remember is the table variables do not have statistics and will be treated by the optimizer as if they have one row, regardless of the actual number of rows they have. I have heard differently at times, but my experience seems to hold this to be true. Best to test for yourself.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1433881
Posted Thursday, March 21, 2013 10:42 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, November 11, 2014 3:53 AM
Points: 1,678, Visits: 19,555
There's an excellent article here



____________________________________________________

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537

Never approach a goat from the front, a horse from the rear, or a fool from any direction.
Post #1433906
Posted Thursday, March 21, 2013 11:42 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 1:13 PM
Points: 40,428, Visits: 36,879
http://sqlinthewild.co.za/index.php/2010/10/12/a-trio-of-table-variables/

The 'table variables are memory only' is an all-too-common misconception. Both temp tables and table variables are allocated space in TempDB and added to the tempDB system tables. They may however not actually be on disk, SQL will try to keep them both in memory as much as possible because it's very likely that those pages will be needed again shortly.



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1433938
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse