Click here to monitor SSC

CTE vs Temporary Tables

Last post 06-22-2010, 11:36 PM by brown. 2 replies.
Sort Posts: Previous Next
  •  04-15-2009, 8:40 AM Post number 72864

    CTE vs Temporary Tables

    I am working with common table expressions and observing what I would consider abnormal behavior. I have created a script that initializes a CTE that contains 22,000 plus rows of data. Later in the script I perform multiple (4) queries filtering the data in the cte. All the resultsets from the 4 queries are returned in one result set via union all clause. This script takes minutes to return the final dataset. I then wrote the same script using a temporary table in place of the cte. This script runs and returns the final result set in seconds.

    I would expect the script using the cte would run faster than the on using a temporoary table. the questions I have is:

    1. Is there an optimum number of rows in a cte after which performance stats to degrade?
    2. Is there a server setting that will optimize SQL server performance for cte's?

  •  04-18-2009, 1:40 PM Post number 73199 in reply to post number 72864

    Re: CTE vs Temporary Tables

    I've found the same problem with CTEs. I'd love to hear from someone who has got them to perform reasonably with  'production-size' data.
  •  06-22-2010, 11:36 PM Post number 93186 in reply to post number 72864

    • brown is not online. Last active: 06-22-2010, 11:39 PM brown
    • Top 500 Contributor
    • Joined on 06-22-2010
    • Level 1: Deep thought

    Re: CTE vs Temporary Tables

    I found very interesting information in it and it is really helpful i think.
View as RSS news feed in XML