Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

CTE Documentation Expand / Collapse
Author
Message
Posted Wednesday, December 19, 2012 9:09 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Sunday, August 17, 2014 3:10 PM
Points: 2,787, Visits: 6,098
I've always understood that CTEs were similar to derived tables in that they were simply logical constructs that the optimizer would turn into a query plan. In explaining CTEs, the analogy I use most often is that they are like a view that persists only for the duration of the single query in which it is contained. The use of a CTE does not, by itself, require any tempdb resources. A query plan might spill over to tempdb on a sort or something, but not merely because a CTE was used in the query.

While I hold these truths to be self-evident, I've just spent an hour or so online trying to find any statement to this effect in BOL. I can find articles and questions answered in forums, but nothing from Microsoft itself. I've also taken a look at the SQL Server 2008 Internals manual but can't find anything.

Could someone point me in the right direction for documentation that a CTE is like an inline view?

P.S. I will be continuing my search and will post an answer if I find it first.


__________________________________________________

Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? -- Stephen Stills
Post #1398755
Posted Wednesday, December 19, 2012 9:14 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: 2 days ago @ 7:58 PM
Points: 3,422, Visits: 5,366
I typically use exactly the same analogies (non-persistent VIEW or derived tables) but unfortunately I can't be helpful and point you to a link that agrees with us.

However I recently found out that CTEs are supported in Oracle SQL. So I wonder if the analogy might be presented in some sort of ISO/ANSI standards documentation on the topic (assuming there is an ISO or ANSI standard for CTEs).



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1398756
Posted Wednesday, December 19, 2012 9:24 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Sunday, August 17, 2014 3:10 PM
Points: 2,787, Visits: 6,098
Thanks, Dwain. Here's the only thing close I've found so far. I didn't realize it was in there until i retreated to the 2005 new features. The text below is copied from

2008 R2 Using Common Table Expressions

A common table expression (CTE) can be thought of as a temporary result set that is defined within the execution scope of a single SELECT, INSERT, UPDATE, DELETE, or CREATE VIEW statement. A CTE is similar to a derived table in that it is not stored as an object and lasts only for the duration of the query....

A CTE can be used to:

...
Substitute for a view when the general use of a view is not required; (Emphasis mine)
...

Does anyone know of any other authority to add to that?


__________________________________________________

Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? -- Stephen Stills
Post #1398762
Posted Wednesday, December 19, 2012 9:43 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: 2 days ago @ 7:58 PM
Points: 3,422, Visits: 5,366
Wiki seems to support us:

Common Table Expressions



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1398767
Posted Wednesday, December 19, 2012 9:45 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Sunday, August 17, 2014 3:10 PM
Points: 2,787, Visits: 6,098
Thanks again, Dwain. I'll keep looking in the morning.

__________________________________________________

Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? -- Stephen Stills
Post #1398769
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse