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 «««1234

Temp Table 'vs' Table Variable 'vs' CTE. Expand / Collapse
Author
Message
Posted Friday, August 21, 2009 11:15 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 4:20 AM
Points: 15,662, Visits: 28,056
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.


----------------------------------------------------
"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 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #775279
Posted Friday, August 21, 2009 12:02 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 4:20 AM
Points: 15,662, Visits: 28,056
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 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #775303
Posted Friday, August 21, 2009 12:31 PM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 2:58 AM
Points: 42,832, Visits: 35,964
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 2008, MVP
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

Post #775326
Posted Friday, August 21, 2009 12:33 PM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Yesterday @ 7:50 PM
Points: 4,351, Visits: 6,167
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 at GMail
Post #775327
Posted Friday, August 21, 2009 12:48 PM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 2:58 AM
Points: 42,832, Visits: 35,964
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 2008, MVP
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

Post #775346
Posted Friday, August 21, 2009 1:02 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 4:20 AM
Points: 15,662, Visits: 28,056
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 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #775354
Posted Friday, August 21, 2009 1:17 PM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Yesterday @ 7:50 PM
Points: 4,351, Visits: 6,167
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 at GMail
Post #775373
Posted Monday, May 6, 2013 3:42 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, July 31, 2014 12:56 PM
Points: 27, Visits: 69
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.


Post #1449912
Posted Monday, May 6, 2013 4:39 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 4:20 AM
Points: 15,662, Visits: 28,056
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 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #1449930
« Prev Topic | Next Topic »

Add to briefcase «««1234

Permissions Expand / Collapse