Understanding CTE and its impacts

This post was written by namwar on January 11, 2008
Posted Under: Uncategorized

Many people get confused or do not properly understand about the capabilities and impacts of Common Table Expressions CTE introduced in SQL Server 2005. Following are the major points to understand the behavior of CTE

  1. Common Table Expressions are essentially a temporary view and do not materialize data like tables.
  2. CTE are not temporary tables therefore, index can not be created on a CTE
  3. CTE do not cause store procedure to be recompiled every time.
  4. CTE are like derived tables, with the addition that they can reference themselves and have recursive capabilities.
  5. CTE are mostly faster then temporary tables due to lack of locking and logging requirements but since temporary tables can have indexes therefore, it depends on the scenario that which strategy will be faster i.e. CTE or Temp tables.
  6. CTE provides clarity in code which reduces the management time and errors.
  7. Same Query written with CTE and without CTE will produce exactly the same results and same Query Plan.

Add a Comment

required, use real name
required, will not be published
optional, your blog address