Table variable vs temp table

  • 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[/url]
    Before posting a performance problem please read[/url]

  • Thanks, nice question

    M&M

  • 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

  • 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?

  • Thanks Ron - I learnt again today

    -------------------------------Posting Data Etiquette - Jeff Moden [/url]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

  • 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.

  • 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

  • This was removed by the editor as SPAM

  • 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

  • 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.

  • Very interesting question 🙂

    In my case on SQL 2008 R2 i receive follow result:

    - table variable - 29290 ms

    - temp table - 240 ms

  • dgvozdetsky (10/3/2011)


    Very interesting question 🙂

    In my case on SQL 2008 R2 i receive follow result:

    - table variable - 29290 ms

    - temp table - 240 ms

    Cool. The comment block in the start of the question have both 2005 and 2008 reference so didnt really know if that would effect anything. And since i dont have a 2008 (let alone an R2). Hard to test.

    I gave it a try on 2000 and that was faster for me (like 70ms +/- 10ms). Strange effect on 2008 i must say.

    /T

  • The site which i referred regarding this says that table variables are stored only in memory. so the access time for a table variable can be faster than the time it takes to access a temporary table.

    Note: http://searchsqlserver.techtarget.com/tip/Temporary-tables-in-SQL-Server-vs-table-variables

    but when i tried to execute the query it results both as same....

    So which one is the correct answer...

  • tommyh (10/3/2011) Strange effect on 2008 i must say.

    /T

    As known, Microsoft was changed work with tempDB since SQL 2005, so it is possible, that time dimensions in SQL 2000 differ with time dimensions in SQL 2k5, 2k8, 2k11 etc.

    Excuse my bad english 🙂

  • Missed it. Thanks for the question.

    http://brittcluff.blogspot.com/

Viewing 15 posts - 1 through 15 (of 52 total)

You must be logged in to reply to this topic. Login to reply