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


Temp Table 'vs' Table Variable 'vs' CTE.


Temp Table 'vs' Table Variable 'vs' CTE.

Author
Message
Grant Fritchey
Grant Fritchey
SSC Guru
SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)

Group: General Forum Members
Points: 93623 Visits: 33010
TheSQLGuru (8/21/2009)

Just to make clear, it isn't JUST about the number of rows you put into a table variable/temp table that matters. I can also be the actual VALUE(s) therein. For example, if you have a table variable with just 1 row, and field1's value is 'abc' and you join that table to another table on field1, you can get horrible performance if the data in the joined table is skewed such that a large percentage of the values are 'abc' due to nested loop plan. A temp table could have stats on field1 and discover that a hash/merge join would be much more efficient.


As Jeff Moden says, test it.

I just ran a series of tests. I'm going to try to put together some more. The basic results were that regardless of the value or the type of table, temp or variable, I got identical execution plans. However, I'm not saying you're wrong. I did see differences in the estimated values, not for the temporary table or the table variable, but for the table they were joined against. The variation wasn't sufficient to result in a different execution plan in the tests I've run so far. I'm going to try for some more extreme data sets to see if I can force a difference.

----------------------------------------------------
The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood...
Theodore Roosevelt

The Scary DBA
Author of: SQL Server Query Performance Tuning and SQL Server Execution Plans
Product Evangelist for Red Gate Software
GilaMonster
GilaMonster
SSC Guru
SSC Guru (211K reputation)SSC Guru (211K reputation)SSC Guru (211K reputation)SSC Guru (211K reputation)SSC Guru (211K reputation)SSC Guru (211K reputation)SSC Guru (211K reputation)SSC Guru (211K reputation)

Group: General Forum Members
Points: 211278 Visits: 46255
If you do get a difference, please blog it. Also, if you do get a difference, try OPTION (RECOMPILE) on the query, see if there's any change. Shouldn't be, recompile just lets the optimiser know the number of rows for table var, not the data distribution, but should be included for completeness.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


TheSQLGuru
TheSQLGuru
SSC-Dedicated
SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)

Group: General Forum Members
Points: 30434 Visits: 8667
Grant Fritchey (8/21/2009)
TheSQLGuru (8/21/2009)

Just to make clear, it isn't JUST about the number of rows you put into a table variable/temp table that matters. I can also be the actual VALUE(s) therein. For example, if you have a table variable with just 1 row, and field1's value is 'abc' and you join that table to another table on field1, you can get horrible performance if the data in the joined table is skewed such that a large percentage of the values are 'abc' due to nested loop plan. A temp table could have stats on field1 and discover that a hash/merge join would be much more efficient.


You sure about that one? I mean the optimizer assumes one row. If it has one row, it's pretty likely to be able to come up with a plan that uses one row correctly, no? That's, to a degree, regardless of what's in the row.


Yes, I am sure, and it is ALL about what is in the row. If you join to a table on 1 (or a few) values and you hit 90% of the rows in the join table do you really want to do that using a nested loop plan??

Here is the typical example I present for this. It uses 3 values (as opposed to a single row - but the reasoning is the same), with distributions shown at the top of the script. AdventureWorks on sql 2005 here


select ProductID, count(*)
from Production.TransactionHistory
group by ProductID
order by count(*)

ProductID
----------- -----------
843 1
849 1
855 1
...
921 2859
873 3003
870 4187

dbcc freeproccache
go
set statistics io on
set statistics time on
go
declare @tab table (a int not null)
insert @tab values (843)
insert @tab values (849)
insert @tab values (855)

select *
from Production.TransactionHistory h
inner join @tab t on t.a = h.ProductID

--18 logical reads, nested loop plan

delete @tab
dbcc freeproccache

insert @tab values (870)
insert @tab values (873)
insert @tab values (921)

select *
from Production.TransactionHistory h
inner join @tab t on t.a = h.ProductID

--30805 logical reads, nested loop plan

dbcc freeproccache

create table #tab (a int not null)
insert #tab values (843)
insert #tab values (849)
insert #tab values (855)

select *
from Production.TransactionHistory h
inner join #tab t on t.a = h.ProductID

--18 logical, nested loop plan
delete #tab

dbcc freeproccache

insert #tab values (870)
insert #tab values (873)
insert #tab values (921)

select *
from #tab t
inner join Production.TransactionHistory h on t.a = h.ProductID

--792 logical reads, hash join plan



Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
GilaMonster
GilaMonster
SSC Guru
SSC Guru (211K reputation)SSC Guru (211K reputation)SSC Guru (211K reputation)SSC Guru (211K reputation)SSC Guru (211K reputation)SSC Guru (211K reputation)SSC Guru (211K reputation)SSC Guru (211K reputation)

Group: General Forum Members
Points: 211278 Visits: 46255
TheSQLGuru (8/21/2009)
If you join to a table on 1 (or a few) values and you hit 90% of the rows in the join table do you really want to do that using a nested loop plan??


Depends which table's the inner and which is the outer. In this case, the small table would be better as the outer table. Though whether the optimiser will do that is another matter.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Grant Fritchey
Grant Fritchey
SSC Guru
SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)

Group: General Forum Members
Points: 93623 Visits: 33010
The data I initially tested with was distributed about the same as yours. I was using AdventureWorks.SalesOrderDetail & the ProductID. 870 has 4,688 rows & 897 has 2 rows. But I kept getting identical execution plans for table variables & temporary tables. I'm planning on bumping up the disparity much more widely to see where it breaks down.

----------------------------------------------------
The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood...
Theodore Roosevelt

The Scary DBA
Author of: SQL Server Query Performance Tuning and SQL Server Execution Plans
Product Evangelist for Red Gate Software
TheSQLGuru
TheSQLGuru
SSC-Dedicated
SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)

Group: General Forum Members
Points: 30434 Visits: 8667
GilaMonster (8/21/2009)
TheSQLGuru (8/21/2009)
If you join to a table on 1 (or a few) values and you hit 90% of the rows in the join table do you really want to do that using a nested loop plan??


Depends which table's the inner and which is the outer. In this case, the small table would be better as the outer table. Though whether the optimiser will do that is another matter.


I forced the order on the loop join with table var and as expected the reads went way down, but I couldn't seem to get the optimizer to pick that plan of it's own volition. I note that the CPU time for the 'correct' plan was 2-4X more than the optimizer-derived one.


select *
from Production.TransactionHistory h
inner join @tab t on t.a = h.ProductID
option (force order)

Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
sivapragasam2000
sivapragasam2000
Valued Member
Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)

Group: General Forum Members
Points: 70 Visits: 83
Old topic but still relavent...

In my previous job, I always used CTEs and they were much faster when compared to temp tables.
In the current place, strangely enough Temp tables almost always win by a mile.
Trust me; what I say is true.

Otherwise, in both places I have been writing Procs for SSRS reports against very similar databases in terms of volume, size and indexes.

From a processing engine stand point would it matter as to how servers are configured, so as to differ in how CTEs and Temp table execution is considered.
Grant Fritchey
Grant Fritchey
SSC Guru
SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)SSC Guru (93K reputation)

Group: General Forum Members
Points: 93623 Visits: 33010
sivapragasam2000 (5/6/2013)
Old topic but still relavent...

In my previous job, I always used CTEs and they were much faster when compared to temp tables.
In the current place, strangely enough Temp tables almost always win by a mile.
Trust me; what I say is true.

Otherwise, in both places I have been writing Procs for SSRS reports against very similar databases in terms of volume, size and indexes.

From a processing engine stand point would it matter as to how servers are configured, so as to differ in how CTEs and Temp table execution is considered.




If you posted this as a new question you'd get a lot more answers.

In a nutshell, yeah, the server configuration matters. Faster/more memory, faster/more CPU, faster/more disks or the opposite absolutely have an affect on queries. That's why so many people just throw hardware at query problems rather than try to take the time to understand the fundamental issues. It really is easier, but usually far less effective.

As to the specifics of your situation, why would a CTE or a temp table be faster? It completely depends on the underlying structures, the indexes, the code, the statistics, and yeah, hardware. Without knowing more I couldn't hazard a guess. But remember that CTE are nothing but queries. No data is stored or special statistics are generated. Temp tables are data storage with statistics. Those differences in behavior will lead to different behaviors of queries in different circumstances.

----------------------------------------------------
The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood...
Theodore Roosevelt

The Scary DBA
Author of: SQL Server Query Performance Tuning and SQL Server Execution Plans
Product Evangelist for Red Gate Software
sivajaggu11
sivajaggu11
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 Visits: 10
Nice explanation.esayly to understand
hi.kumar01
hi.kumar01
Grasshopper
Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)

Group: General Forum Members
Points: 23 Visits: 1
check below url for better clarity.

http://www.ssktutorials.com/2016/06/difference-between-cte-and-temp-table.html
Go


Permissions

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

































































































































































SQLServerCentral


Search