I was looking at the article here Temporary Tables vs. Table Variables and Their Effect on SQL Server Performance and on SQL Server 2008 was able to reproduce similar results to those shown there for 2005.
When executing the stored procedures (definitions below) with only 10 rows the table variable version out performs the temporary table version by more than two times.
I cleared the procedure cache and ran both stored procedures 10,000 times then repeated the process for another 4 runs. Results below (time in ms per batch)
T2_Time V2_Time----------- -----------8578 2718 6641 2781 6469 2813 6766 27976156 2719
My question is: What is the reason for the better performance of the table variable version?
I've done some investigation. e.g. Looking at the performance counters with
SELECT cntr_valuefrom sys.dm_os_performance_counterswhere counter_name = 'Temp Tables Creation Rate';
confirms that in both cases the temporary objects are being cached after the first run as expected rather than created from scratch again for every invocation.
Similarly tracing the Auto Stats
, SP:Recompile
, SQL:StmtRecompile
events in Profiler (screenshot below) shows that these events only occur once (on the first invocation of the #temp
table stored procedure) and the other 9,999 executions do not raise any of these events. (The table variable version does not get any of these events)
Image may be NSFW.
Clik here to view.
The slightly greater overhead of the first run of the stored procedure can in no way account for the big overall difference however as it still only takes a few ms to clear the procedure cache and run both procedures once so I don't believe either statistics or recompiles can be the cause.
Create Required Database Objects
CREATE DATABASE TESTDB_18Feb2012;GOUSE TESTDB_18Feb2012;CREATE TABLE NUM ( n INT PRIMARY KEY, s VARCHAR(128) ); WITH NUMS(N) AS (SELECT TOP 1000000 ROW_NUMBER() OVER (ORDER BY $/0) FROM master..spt_values v1, master..spt_values v2) INSERT INTO NUM SELECT N, 'Value: '+ CONVERT(VARCHAR, N) FROM NUMS GOCREATE PROCEDURE [dbo].[T2] @total INT AS CREATE TABLE #T ( n INT PRIMARY KEY, s VARCHAR(128) ) INSERT INTO #T SELECT n, s FROM NUM WHERE n%100 > 0 AND n <= @total DECLARE @res VARCHAR(128) SELECT @res = MAX(s) FROM NUM WHERE n <= @total AND NOT EXISTS(SELECT * FROM #T WHERE #T.n = NUM.n) GOCREATE PROCEDURE [dbo].[V2] @total INT AS DECLARE @V TABLE ( n INT PRIMARY KEY, s VARCHAR(128)) INSERT INTO @V SELECT n, s FROM NUM WHERE n%100 > 0 AND n <= @total DECLARE @res VARCHAR(128) SELECT @res = MAX(s) FROM NUM WHERE n <= @total AND NOT EXISTS(SELECT * FROM @V V WHERE V.n = NUM.n) GO
Test Script
SET NOCOUNT ON;DECLARE @T1 DATETIME2, @T2 DATETIME2, @T3 DATETIME2, @Counter INT = 0SET @T1 = SYSDATETIME()WHILE ( @Counter < 10000)BEGINEXEC dbo.T2 10SET @Counter += 1ENDSET @T2 = SYSDATETIME()SET @Counter = 0WHILE ( @Counter < 10000)BEGINEXEC dbo.V2 10SET @Counter += 1ENDSET @T3 = SYSDATETIME()SELECT DATEDIFF(MILLISECOND,@T1,@T2) AS T2_Time, DATEDIFF(MILLISECOND,@T2,@T3) AS V2_Time