<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	
	>
<channel>
	<title>Comments on: SQL Server Table Variable vs Temporary Table vs CTE</title>
	<atom:link href="http://thinknook.com/sql-server-table-variable-vs-temporary-table-vs-cte-2012-01-12/feed/" rel="self" type="application/rss+xml" />
	<link>http://thinknook.com/sql-server-table-variable-vs-temporary-table-vs-cte-2012-01-12/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=sql-server-table-variable-vs-temporary-table-vs-cte</link>
	<description>Because the world needs another Business Intelligence blog!</description>
	<lastBuildDate>Fri, 14 Sep 2018 19:30:18 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.41</generator>
	<item>
		<title>By: Links Naji</title>
		<link>http://thinknook.com/sql-server-table-variable-vs-temporary-table-vs-cte-2012-01-12/#comment-3053</link>
		<dc:creator><![CDATA[Links Naji]]></dc:creator>
		<pubDate>Wed, 25 Sep 2013 10:40:57 +0000</pubDate>
		<guid isPermaLink="false">http://thinknook.com/?p=321#comment-3053</guid>
		<description><![CDATA[Hi Andy,

Over-all I tend to agree with your thinking, although there are some subtle ways your hypothesis might be challenged, in relation to how SQL Server&#039;s internals work.

When using CTE for multiple queries, you are correct that SQL Server will perform the CTE multiple times, but you have to remember that SQL tries to cache things in memory, and you might notice that the second time the same CTE is used, most of the data will already be available in memory (the underlying tables would&#039;ve been uploaded to memory), and so it will return much faster.

When it comes to table variables, SQL Server does not necessarily keep the data &quot;in-memory&quot; and could potentially push it off to disk (in tempdb), if this happens, then the cost associated with querying a table variable will obviously increase, and given that you cannot control indexes and statistics, you might end up in a worse situation than using a temp table.

I would even go further, and recommend that maybe you use a local temp table to the database you are operating on, instead of a temp table in tempdb, this will most likely give you a more consistent performance figure, eliminating dependency on existing tempdb operation.]]></description>
		<content:encoded><![CDATA[<p>Hi Andy,</p>
<p>Over-all I tend to agree with your thinking, although there are some subtle ways your hypothesis might be challenged, in relation to how SQL Server&#8217;s internals work.</p>
<p>When using CTE for multiple queries, you are correct that SQL Server will perform the CTE multiple times, but you have to remember that SQL tries to cache things in memory, and you might notice that the second time the same CTE is used, most of the data will already be available in memory (the underlying tables would&#8217;ve been uploaded to memory), and so it will return much faster.</p>
<p>When it comes to table variables, SQL Server does not necessarily keep the data &#8220;in-memory&#8221; and could potentially push it off to disk (in tempdb), if this happens, then the cost associated with querying a table variable will obviously increase, and given that you cannot control indexes and statistics, you might end up in a worse situation than using a temp table.</p>
<p>I would even go further, and recommend that maybe you use a local temp table to the database you are operating on, instead of a temp table in tempdb, this will most likely give you a more consistent performance figure, eliminating dependency on existing tempdb operation.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andy</title>
		<link>http://thinknook.com/sql-server-table-variable-vs-temporary-table-vs-cte-2012-01-12/#comment-3051</link>
		<dc:creator><![CDATA[Andy]]></dc:creator>
		<pubDate>Fri, 13 Sep 2013 08:46:16 +0000</pubDate>
		<guid isPermaLink="false">http://thinknook.com/?p=321#comment-3051</guid>
		<description><![CDATA[Maybe you can confirm or disprove something which I thought I heard (relating CTE&#039;s and Table Variables).
I believe that if you are using a CTE for multiple queries, then each reference of the CTE will go away and perform the query again.
If you use a table variable, once you have populated this, then the data is &quot;in-memory&quot;.  Subsequently, if there are multiple references, then it is more efficient to use a table variable than a CTE.
Similarly, depending on the number of rows in your table variable, a temp table will become more efficient as it can be indexed and has statistics.
Not sure if that&#039;s all utter nonsense, however this is how I&#039;ve been lead to believe the differences are.]]></description>
		<content:encoded><![CDATA[<p>Maybe you can confirm or disprove something which I thought I heard (relating CTE&#8217;s and Table Variables).<br />
I believe that if you are using a CTE for multiple queries, then each reference of the CTE will go away and perform the query again.<br />
If you use a table variable, once you have populated this, then the data is &#8220;in-memory&#8221;.  Subsequently, if there are multiple references, then it is more efficient to use a table variable than a CTE.<br />
Similarly, depending on the number of rows in your table variable, a temp table will become more efficient as it can be indexed and has statistics.<br />
Not sure if that&#8217;s all utter nonsense, however this is how I&#8217;ve been lead to believe the differences are.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: admin</title>
		<link>http://thinknook.com/sql-server-table-variable-vs-temporary-table-vs-cte-2012-01-12/#comment-491</link>
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Wed, 01 Aug 2012 17:13:26 +0000</pubDate>
		<guid isPermaLink="false">http://thinknook.com/?p=321#comment-491</guid>
		<description><![CDATA[&quot;DOS it&quot; --&gt; thats just brilliant, particularly because I live in Scotland and &lt;a href=&quot;http://dos.urbanup.com/1096778&quot; window=&quot;_blank&quot; rel=&quot;nofollow&quot;&gt;&quot;dos&quot; (urban dic)&lt;/a&gt; has a different meaning.

He actually meant BCP as in &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/ms162802.aspx&quot; window=&quot;_blank&quot; rel=&quot;nofollow&quot;&gt;BULK COPY PROCEDURE&lt;/a&gt; in SQL Server :)]]></description>
		<content:encoded><![CDATA[<p>&#8220;DOS it&#8221; &#8211;> thats just brilliant, particularly because I live in Scotland and <a href="http://dos.urbanup.com/1096778" window="_blank" rel="nofollow">&#8220;dos&#8221; (urban dic)</a> has a different meaning.</p>
<p>He actually meant BCP as in <a href="http://msdn.microsoft.com/en-us/library/ms162802.aspx" window="_blank" rel="nofollow">BULK COPY PROCEDURE</a> in SQL Server <img src="http://thinknook.com/wp-includes/images/smilies/icon_smile.gif" alt=":)" class="wp-smiley" /></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Forrest</title>
		<link>http://thinknook.com/sql-server-table-variable-vs-temporary-table-vs-cte-2012-01-12/#comment-490</link>
		<dc:creator><![CDATA[Forrest]]></dc:creator>
		<pubDate>Wed, 01 Aug 2012 16:48:36 +0000</pubDate>
		<guid isPermaLink="false">http://thinknook.com/?p=321#comment-490</guid>
		<description><![CDATA[Perhaps he wants to open all the table data in his word processor where he can understand it? Did he at least know what a command prompt is? Perhaps he learned that telling you to DOS it was just silly...]]></description>
		<content:encoded><![CDATA[<p>Perhaps he wants to open all the table data in his word processor where he can understand it? Did he at least know what a command prompt is? Perhaps he learned that telling you to DOS it was just silly&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: admin</title>
		<link>http://thinknook.com/sql-server-table-variable-vs-temporary-table-vs-cte-2012-01-12/#comment-488</link>
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Wed, 01 Aug 2012 08:23:14 +0000</pubDate>
		<guid isPermaLink="false">http://thinknook.com/?p=321#comment-488</guid>
		<description><![CDATA[Haha awesome.

I had a manager a while back that used to always suggest to &quot;BCP&quot; everything!, even if we were talking about something completely unrelated, he would still suggest to BCP it... I started thinking that he&#039;s just using it as a buzz word, hoping 1 in 100 it might hit home. :)]]></description>
		<content:encoded><![CDATA[<p>Haha awesome.</p>
<p>I had a manager a while back that used to always suggest to &#8220;BCP&#8221; everything!, even if we were talking about something completely unrelated, he would still suggest to BCP it&#8230; I started thinking that he&#8217;s just using it as a buzz word, hoping 1 in 100 it might hit home. <img src="http://thinknook.com/wp-includes/images/smilies/icon_smile.gif" alt=":)" class="wp-smiley" /></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: forrest</title>
		<link>http://thinknook.com/sql-server-table-variable-vs-temporary-table-vs-cte-2012-01-12/#comment-487</link>
		<dc:creator><![CDATA[forrest]]></dc:creator>
		<pubDate>Wed, 01 Aug 2012 03:15:22 +0000</pubDate>
		<guid isPermaLink="false">http://thinknook.com/?p=321#comment-487</guid>
		<description><![CDATA[Thanks for that post. I was trying to figure out some of the methods I should try to use to approach an array like situation, and all the advise was throwing about the term CTE like it was the messiah come back.... As I suspected - fancy sub queries it is then.  ;)]]></description>
		<content:encoded><![CDATA[<p>Thanks for that post. I was trying to figure out some of the methods I should try to use to approach an array like situation, and all the advise was throwing about the term CTE like it was the messiah come back&#8230;. As I suspected &#8211; fancy sub queries it is then.  <img src="http://thinknook.com/wp-includes/images/smilies/icon_wink.gif" alt=";)" class="wp-smiley" /></p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.w3-edge.com/products/


Served from: thinknook.com @ 2026-04-15 07:25:31 by W3 Total Cache
-->