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 12345»»»

Table variable vs temp table Expand / Collapse
Author
Message
Posted Sunday, October 2, 2011 8:42 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, September 18, 2014 7:50 PM
Points: 5,322, Visits: 25,235
Comments posted to this topic are about the item Table variable vs temp table

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
Post #1184177
Posted Monday, October 3, 2011 12:09 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, September 8, 2014 6:15 PM
Points: 2,278, Visits: 3,793
Thanks, nice question

Mohammed Moinudheen
Post #1184280
Posted Monday, October 3, 2011 12:16 AM
UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: Sunday, June 29, 2014 11:26 PM
Points: 1,481, Visits: 1,960
There are several problems with this question. One being that you cant declare @Start 2 times.

The other being that the answer is wrong. They both perform equally. I have executed the query as is (though with the @Start thing dealt with). With the 2 queries in the opposit order and one at a time. And the time is always roughly 100ms result (give or take 10ms).

I get the same execution plan (2005) for both queries. So im hoping that someone with more knowledge then me can enlighten us.

The above ramblings can be ignored. Rewriting it i had missed that there are 2! selects. Running the code from the link was "better". And had the effect that was being pointed out.

/T
Post #1184284
Posted Monday, October 3, 2011 12:53 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Tuesday, April 1, 2014 2:54 AM
Points: 443, Visits: 71
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/

Confused on which one is better?
Post #1184294
Posted Monday, October 3, 2011 2:03 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 6:44 AM
Points: 3,068, Visits: 3,290
Thanks Ron - I learnt again today

-------------------------------
Posting Data Etiquette - Jeff Moden
Smart way to ask a question

There are naive questions, tedious questions, ill-phrased questions, questions put after inadequate self-criticism. But every question is a cry to understand (the world). There is no such thing as a dumb question. ― Carl Sagan
I would never join a club that would allow me as a member - Groucho Marx
Post #1184332
Posted Monday, October 3, 2011 2:42 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, September 17, 2014 12:53 AM
Points: 2,529, Visits: 2,402
tommyh (10/3/2011)
There are several problems with this question. One being that you cant declare @Start 2 times.

The other being that the answer is wrong. They both perform equally. I have executed the query as is (though with the @Start thing dealt with). With the 2 queries in the opposit order and one at a time. And the time is always roughly 100ms result (give or take 10ms).

I get the same execution plan (2005) for both queries. So im hoping that someone with more knowledge then me can enlighten us.

/T

You should execute the two batch separately, so, you can declare @Start twice.
But, I agree with you: the answere is wrong and hardly to establish which perform better.
Post #1184344
Posted Monday, October 3, 2011 2:51 AM
UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: Sunday, June 29, 2014 11:26 PM
Points: 1,481, Visits: 1,960
Carlo Romagnano (10/3/2011)
tommyh (10/3/2011)
There are several problems with this question. One being that you cant declare @Start 2 times.

The other being that the answer is wrong. They both perform equally. I have executed the query as is (though with the @Start thing dealt with). With the 2 queries in the opposit order and one at a time. And the time is always roughly 100ms result (give or take 10ms).

I get the same execution plan (2005) for both queries. So im hoping that someone with more knowledge then me can enlighten us.

/T

You should execute the two batch separately, so, you can declare @Start twice.
But, I agree with you: the answere is wrong and hardly to establish which perform better.


I did do that too... but i also wanted to run the code "as is" incase it was something that was dependent on them being run both at the same time.

/T
Post #1184350
Posted Monday, October 3, 2011 2:52 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 5:39 AM
Points: 3,966, Visits: 5,206
Interesting question, Ron.
Thanks

Something to bear in mind: There are numerous factors that influence the choice between table variables and/or temp tables, inter alia:
> Data set size (number of columns and rows) - larger datasets work better with temp tables (mainly because these can be indexed);
> Availalbe memory vs available drive space (file swapping etc);
> IO's;
> Persistence (using the data set in other stored procedures)
> UDF - one can only use table variables in UDF's


____________________________________________
Space, the final frontier? not any more...
All limits henceforth are self-imposed.
“libera tute vulgaris ex”
Post #1184351
Posted Monday, October 3, 2011 3:04 AM
UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: Sunday, June 29, 2014 11:26 PM
Points: 1,481, Visits: 1,960
stewartc-708166 (10/3/2011)
Interesting question, Ron.
Thanks

Something to bear in mind: There are numerous factors that influence the choice between table variables and/or temp tables, inter alia:
> Data set size (number of columns and rows) - larger datasets work better with temp tables (mainly because these can be indexed);
> Availalbe memory vs available drive space (file swapping etc);
> IO's;
> Persistence (using the data set in other stored procedures)
> UDF - one can only use table variables in UDF's


The first 2 points are "wrongish". http://sqlinthewild.co.za/index.php/2010/10/12/a-trio-of-table-variables/

/T
Post #1184358
Posted Monday, October 3, 2011 3:04 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, January 24, 2013 9:59 PM
Points: 1,354, Visits: 1,299
tommyh (10/3/2011)
There are several problems with this question. One being that you cant declare @Start 2 times.

The other being that the answer is wrong. They both perform equally. I have executed the query as is (though with the @Start thing dealt with). With the 2 queries in the opposit order and one at a time. And the time is always roughly 100ms result (give or take 10ms).

I get the same execution plan (2005) for both queries. So im hoping that someone with more knowledge then me can enlighten us.

/T

I agree that they would both perform equally and the answer is wrong. The referenced link was regarding stored procedures and the example code in the QOTD was not a stored procedure. Suppose that makes a difference because it would need to be recompiled in a stored procedure but if you're running it for the first time as a non-stored procedure query, they both need to be parsed and an execution plan created anyway.
Post #1184359
« Prev Topic | Next Topic »

Add to briefcase 12345»»»

Permissions Expand / Collapse