SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

CTE and Spool operators

CTE and Spool operators

patrickmcginnis59 10839
patrickmcginnis59 10839
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1786 Visits: 5527
456789psw (4/9/2013)
I commonly find CTE using spool operators in the execution plan. According to Microsoft the spool operator uses the temp table.

"The Table Spool operator scans the input and places a copy of each row in a hidden spool table that is stored in the tempdb database and existing only for the lifetime of the query. If the operator is rewound (for example, by a Nested Loops operator) but no rebinding is needed, the spooled data is used instead of rescanning the input."

So this leads to believe its possible that CTE's are using tempdb? so are the results stored in memory or tempdb? or does is DEPEND?

I like to in my own mind to separate the entire concept of CTE's from the eventual execution of the query, like what is described in programming language translation textbooks. In my own invented theory of SQL server internal workings, the query starts as a simple stream of text we type in, which is then split into keywords, object references and operators (with delimiters helping us along in splitting this text into keywords, references and operators (or "tokens" in interpreterspeak)).

Next, these keywords, references and operators are put together into syntactical constructs that are "generic" in nature with parameters, like <KEYWORD-SELECT>,<COLUMN>,[(optionally zero or more occuring instances of <COMMA-SEPARATOR><COLUMN>]<KEYWORD-FROM><TABLE-OR-VIEW-OR-CTE-OR-WHATEVER> etc etc and from this we have a symbolic representation of operations we want the server to execute.

Now AT THIS POINT the server can make logically equivalent substitutions (and will evaluate these substitutions using rankings and including statistics and calculations that can be derived by data stored concerning these objects). Often at this point, CTE's could be eliminated entirely as they could be replaced by syntactical constructs that the original text has described during the original typing of the query, or these could have been completely replaced by logically equivalent constructs. Programmingwise, at some point, the server has evaluated some number of these various logically equivalent substitutions (plans) and its going to have one that estimates to execute best according to statistics and will pass a compiled execution plan to the database execution engine (an invented name for some blob of software internal to SQL server).

Now during the execution of this intermediate coded little programs (internal executable descriptions of those query plans (which is really probably just some sort of program in itself), temp results may spill. But I like to think that the decision in the server software to spill results to tempdb is SO FAR REMOVED from the phase of program translation that trying to link CTE's to whether a query will spill temp results to tempdb that its really sort of a moot point.

Obviously, this is just a pipedream crackpot theory of mine, Microsoft could actually have a squirrel running on a treadmill for all I know, but regardless I like to think along those terms when thinking about questions like this.

to properly post on a forum:


You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum