8

How and why CTE gives a better performance as compared to derived table/ subqueries/ temp table etc. approaches?

Any temporary calculations happens in the temporary database. So if we have a cursor approach, it also creates temporary table/work table in the temporary database and once the operation is over, that work table is destroyed. My understanding of CTE is that, it also does the same(or does it creates temporary result in memory? and hence the performance improvement) Then why is it better than the above approaches like cursor/derived table/ subqueries/ temp table etc.?

5
  • 2
    specific example? that is a rather sweeping statement that is untrue. Commented Jul 26, 2011 at 2:46
  • In general i am looking for the answer... nothing specific...
    – mcUser
    Commented Jul 26, 2011 at 2:48
  • 3
    There is nothing magic about CTES that make them faster or slower than the equivalent query with subquery. There are use cases that are only possible efficiently with CTES such as recursion, but its severe edge case. Commented Jul 26, 2011 at 2:51
  • 5
    The main reason is this: cursors are a procedural construct - you create it, you step through it one by one. This is fine for general-purpose programming - but it's totally against the set-based thinking and working of SQL Server. Cursors are memory and performance hogs (in most cases), so be very very careful where and when you use them. 90% or more of the time you don't really need a cursor - you could do it just as fine (or much better!) with a set-based approach
    – marc_s
    Commented Jul 26, 2011 at 4:59
  • 2
    @marc_s: of course, under the covers, the set based approach uses loops to process resultsets, BUT the big difference is these are SQL Server's internal, fast loops (Please don't let anyone take that to mean I'm advocating using cursors. Don't!) Commented Jul 26, 2011 at 5:02

1 Answer 1

12

A (non-recursive) CTE does not use cursors. It is a set based approach. That's the big difference compared to using cursors. But then that's true of not using cursors in general.

Cursors should be avoided where absolutely possible (as I'm sure we are all aware).

A CTE is not necessarily better than using a derived table, but does lead to more understandable TSQL code. A CTE is really just shorthand for a query or subquery; something akin to a temporary view.

The situation where CTE's might not be the best approach, is when the query plan optimiser gets inaccurate row estimates for the CTE.

Related question: What are the advantages/disadvantages of using a CTE?

6
  • 7
    +1 Cursors are the devil's work - stay away from them as much as you can! Especially in triggers ... (I have such a devilish database to deal with right now....)
    – marc_s
    Commented Jul 26, 2011 at 4:57
  • 2
    yes, inside a trigger - and inside that cursor, another table is updated and another trigger fires.... with another cursor inside..... this database should really be copied to DEV:NULL for good....
    – marc_s
    Commented Jul 26, 2011 at 5:02
  • 1
    If the ACM or anyone ever needs an example of a truly horrific database doing all things that are forbidden and frowned upon - got a great example here :-)
    – marc_s
    Commented Jul 26, 2011 at 5:04
  • And then people asked "why is it slow?" :p. I went from a cursor approach to 8 layers of CTE's on top of eachother (the final UPDATE statement being layer 9) and improved a 6 hours batch to 4 minutes !!! Commented May 3, 2012 at 10:12
  • 2
    @barrypicker, Readability was alright (it was always going to be complex). The layers were mostly necessary so you could define a value x as being column1 * column2 (actually a funky case-statement) and then, in layer2 you can use x in another calculation without doubling up (and breaking the DRY principle). Commented Mar 4, 2014 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.