Temp Table Vs Table Variable

  • Hi Guys,

    Any one please let me know which one is better in memory utilizations in sql server 2005

    Thanks in Advance....

  • Neither, not a factor in this debate.



    Clear Sky SQL
    My Blog[/url]

  • Not in memory utilization but off course in terms of its existence (life) in memory and Execution Plan re-usability (recompilation threshold) Table Variables are better.

  • Dev (12/7/2011)


    in terms of its existence (life) in memory and Execution Plan re-usability (recompilation threshold) Table Variables are better.

    As long as you don't mind poor-terrible performance in return (in many cases)

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    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
  • GilaMonster (12/7/2011)


    Dev (12/7/2011)


    in terms of its existence (life) in memory and Execution Plan re-usability (recompilation threshold) Table Variables are better.

    As long as you don't mind poor-terrible performance in return (in many cases)

    Yup... Pros & Cons 🙂

  • In short, you should use Table Variables when it has to contain very small chunk of temporary data (i.e. few rows only). If the temporary data size is huge, you should go with Temporary Tables.


    Sujeet Singh

  • hope both gives better performanace than cursors.

    Regards
    Durai Nagarajan

  • durai nagarajan (12/7/2011)


    hope both gives better performanace than cursors.

    It depends on what you're doing with them.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • Divine Flame (12/7/2011)


    In short, you should use Table Variables when it has to contain very small chunk of temporary data (i.e. few rows only). If the temporary data size is huge, you should go with Temporary Tables.

    I've seen a proc go from 50M reads to less than 2M.

    That 1 single @t had only 2 rows in it.

  • Ninja's_RGR'us (12/7/2011)


    Divine Flame (12/7/2011)


    In short, you should use Table Variables when it has to contain very small chunk of temporary data (i.e. few rows only). If the temporary data size is huge, you should go with Temporary Tables.

    I've seen a proc go from 50M reads to less than 2M.

    That 1 single @t had only 2 rows in it.

    That's an eye opener :w00t: I don't know what developer was doing with those 2 rows :hehe:


    Sujeet Singh

  • Divine Flame (12/7/2011)


    Ninja's_RGR'us (12/7/2011)


    Divine Flame (12/7/2011)


    In short, you should use Table Variables when it has to contain very small chunk of temporary data (i.e. few rows only). If the temporary data size is huge, you should go with Temporary Tables.

    I've seen a proc go from 50M reads to less than 2M.

    That 1 single @t had only 2 rows in it.

    That's an eye opener :w00t: I don't know what developer was doing with those 2 rows :hehe:

    WHERE Items.ItemCategoryCode IN (SELECT CategoryCode FROM @CaTs)

    Convert to #tbl with PK to help the stats, search and replace to find the 5-10 occurances and boom, 98% reads improvements ;-). Around 50% duration improvement as well.

  • Ninja's_RGR'us (12/7/2011)


    WHERE Items.ItemCategoryCode IN (SELECT CategoryCode FROM @CaTs)

    Convert to #tbl with PK to help the stats, search and replace to find the 5-10 occurances and boom, 98% reads improvements ;-). Around 50% duration improvement as well.

    Will Table Variable @tbl with PK not work?

  • Dev (12/7/2011)


    Ninja's_RGR'us (12/7/2011)


    WHERE Items.ItemCategoryCode IN (SELECT CategoryCode FROM @CaTs)

    Convert to #tbl with PK to help the stats, search and replace to find the 5-10 occurances and boom, 98% reads improvements ;-). Around 50% duration improvement as well.

    Will Table Variable @tbl with PK not work?

    No stats on @t.

    That made the whole difference.

  • Ninja's_RGR'us (12/7/2011)


    Dev (12/7/2011)


    Ninja's_RGR'us (12/7/2011)


    WHERE Items.ItemCategoryCode IN (SELECT CategoryCode FROM @CaTs)

    Convert to #tbl with PK to help the stats, search and replace to find the 5-10 occurances and boom, 98% reads improvements ;-). Around 50% duration improvement as well.

    Will Table Variable @tbl with PK not work?

    No stats on @t.

    That made the whole difference.

    Do we really need stats for 2 rows? 😀

  • Dev (12/7/2011)


    Ninja's_RGR'us (12/7/2011)


    Dev (12/7/2011)


    Ninja's_RGR'us (12/7/2011)


    WHERE Items.ItemCategoryCode IN (SELECT CategoryCode FROM @CaTs)

    Convert to #tbl with PK to help the stats, search and replace to find the 5-10 occurances and boom, 98% reads improvements ;-). Around 50% duration improvement as well.

    Will Table Variable @tbl with PK not work?

    No stats on @t.

    That made the whole difference.

    Do we really need stats for 2 rows? 😀

    YES. Especially in filters. This will byte you in the arse so hard when you filter on skewed data distribution.

    1 filter might return 1 % of the data, but the next one 25%. The QO needs to be able to know about that. Can't do it without stats on that #tbl. The PK then tells him that there's no duplicate value (especially useful in joins).

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

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