ms sql server - #temp table performance....

Posted on 2011-10-13
Last Modified: 2012-05-12
we've got #temp_tables that dont use the proper syntax:


But select right into it

Select x, y,z
      Into #TempTable
      From (
      SELECT .....

it works..but I think that there would be a performance drg, plus there are any DROP TABLE #temp_table at the end of the the intention is that SQL SVR will blow it away once the SP is done excuting...but we have had performance issues...

does it make that big of a difference?
Question by:GlobaLevel
    LVL 25

    Accepted Solution

    Drop is not necessairy but always nice to have it.
    Create is better (and more understandable in future)

    Temp_tables consume write-time in tempdb and mostly don't have indexes so if they contain a large number of rows it will be slow. So when you reference that same temptable several times it could be worth creating an index.
    Already considered a table-variable?  when indexes are not needed see here for some advantages or here for the comparison
    LVL 25

    Expert Comment

    The temp table you are creating at the beginning is of no use, considering the fact that there are no data inserted into it apart from the select into. Both temp table, the one manually created and the other with select into spells differently. So, create statement can be commented out.
    When you are saying performance problem, what do you mean to say? Are you seeing any tempdb contention, where you get waiting resource to be something 2:x:x ?
    And as jogos said, its better to create indexes on the table.
    LVL 25

    Expert Comment

    Maybe this link can show you why temp tables are not always a good idea

    Using the create-statement is better
    - for documentation, it's better to see what you are doing and with which datatypes you are working
    - when creating it in the in the insert-statement you lock system tables (table definition in temp-db) for the duration of your insert-statement so you could be holding up other processes when it's about a lot of records

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
    'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now