CTE results storage same as temp table?

  • Simple question, are the results of a CTE stored in the same place as a temp table (tempdb)?

    Thanks

  • smallmoney (1/26/2014)


    Simple question, are the results of a CTE stored in the same place as a temp table (tempdb)?

    Thanks

    As with a View or any query, for that matter, they might not be stored at all. Proof of that is if you call the CTE twice as a self join. If you look at the execution plan, you'll see that the CTE actually had to run twice. Any interim storage required by a query, for a working table to support a hash join for example, will make use of TempDB.

    That being said, TempDB is NOT disk only. If the optimizer can and does allocate memory for the task, the TempDB part of it will be done in memory. Similarly (and just to be sure), Table Variables are NOT memory only. They will spill to disk if they get too big.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • CTEs aren't tables. They're named subqueries. Their results are not necessarily materialised or stored anywhere.

    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

Viewing 3 posts - 1 through 2 (of 2 total)

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