Why is CTE better than cursor/derived table/ subqueries/ temp table etc.?

前端 未结 1 1678
遇见更好的自我
遇见更好的自我 2020-12-18 07:10

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

相关标签:
1条回答
  • 2020-12-18 07:38

    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?

    0 讨论(0)
提交回复
热议问题