SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Table variable vs temp table


Table variable vs temp table

Author
Message
bitbucket-25253
bitbucket-25253
SSCertifiable
SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)

Group: General Forum Members
Points: 7771 Visits: 25280
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
M&M
M&M
Hall of Fame
Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)

Group: General Forum Members
Points: 3503 Visits: 3906
Thanks, nice question

M&M
tommyh
tommyh
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1762 Visits: 2000
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
yogesh.balasubramanian
yogesh.balasubramanian
SSC-Addicted
SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)

Group: General Forum Members
Points: 446 Visits: 72
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/

Unsure Confused on which one is better?
Stuart Davies
Stuart Davies
SSCertifiable
SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)

Group: General Forum Members
Points: 5268 Visits: 4682
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
Carlo Romagnano
Carlo Romagnano
SSCarpal Tunnel
SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)

Group: General Forum Members
Points: 4846 Visits: 3325
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 run on tuttopodismo
tommyh
tommyh
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1762 Visits: 2000
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
Stewart "Arturius" Campbell
Stewart "Arturius" Campbell
SSCrazy Eights
SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)

Group: General Forum Members
Points: 8751 Visits: 7279
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”
tommyh
tommyh
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1762 Visits: 2000
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
cengland0
cengland0
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1666 Visits: 1300
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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search