I created this test codeSET NOCOUNT ON-- Prepare sample dataCREATE TABLE #Table1 ( NID INT PRIMARY KEY CLUSTERED )INSERT #Table1SELECT NumberFROM F_TABLE_NUMBER_RANGE(0, 99999)CREATE TABLE #Table2 ( ID INT PRIMARY KEY CLUSTERED, NID INT, VoterID INT )INSERT #Table2SELECT Number, ABS(CAST(CAST(NEWID() AS VARBINARY(32)) AS BIGINT)) % 100000, ABS(CAST(CAST(NEWID() AS VARBINARY(32)) AS BIGINT)) % 200FROM F_TABLE_NUMBER_RANGE(0, 999999)CREATE TABLE #Times ( TryName VARCHAR(20), ms INT )GO-- Start time testingDBCC FREEPROCCACHEDBCC DROPCLEANBUFFERSDECLARE @i INT, @dt DATETIME, @d INTSET @i = 101WHILE @i > 1 BEGIN SET @dt = CURRENT_TIMESTAMP SELECT @d = COUNT(*) FROM #Table1 AS t1 WHERE NOT EXISTS (SELECT t2.NID FROM #Table2 AS t2 WHERE t2.NID = t1.NID AND t2.VoterID = 5) INSERT #Times VALUES ('Try 1 with t2.NID', DATEDIFF(MILLISECOND, @dt, CURRENT_TIMESTAMP)) SET @i = @i - 1 ENDGODBCC FREEPROCCACHEDBCC DROPCLEANBUFFERSDECLARE @i INT, @dt DATETIME, @d INTSET @i = 101WHILE @i > 1 BEGIN SET @dt = CURRENT_TIMESTAMP SELECT @d = COUNT(*) FROM #Table1 AS t1 WHERE NOT EXISTS (SELECT 0 FROM #Table2 AS t2 WHERE t2.NID = t1.NID AND t2.VoterID = 5) INSERT #Times VALUES ('Try 2 with 0', DATEDIFF(MILLISECOND, @dt, CURRENT_TIMESTAMP)) SET @i = @i - 1 ENDGODBCC FREEPROCCACHEDBCC DROPCLEANBUFFERSDECLARE @i INT, @dt DATETIME, @d INTSET @i = 101WHILE @i > 1 BEGIN SET @dt = CURRENT_TIMESTAMP SELECT @d = COUNT(*) FROM #Table1 AS t1 WHERE NOT EXISTS (SELECT 1 FROM #Table2 AS t2 WHERE t2.NID = t1.NID AND t2.VoterID = 5) INSERT #Times VALUES ('Try 3 with 1', DATEDIFF(MILLISECOND, @dt, CURRENT_TIMESTAMP)) SET @i = @i - 1 ENDGODBCC FREEPROCCACHEDBCC DROPCLEANBUFFERSDECLARE @i INT, @dt DATETIME, @d INTSET @i = 101WHILE @i > 1 BEGIN SET @dt = CURRENT_TIMESTAMP SELECT @d = COUNT(*) FROM #Table1 AS t1 WHERE NOT EXISTS (SELECT NULL FROM #Table2 AS t2 WHERE t2.NID = t1.NID AND t2.VoterID = 5) INSERT #Times VALUES ('Try 4 with NULL', DATEDIFF(MILLISECOND, @dt, CURRENT_TIMESTAMP)) SET @i = @i - 1 ENDGODBCC FREEPROCCACHEDBCC DROPCLEANBUFFERSDECLARE @i INT, @dt DATETIME, @d INTSET @i = 101WHILE @i > 1 BEGIN SET @dt = CURRENT_TIMESTAMP SELECT @d = COUNT(*) FROM #Table1 AS t1 WHERE NOT EXISTS (SELECT * FROM #Table2 AS t2 WHERE t2.NID = t1.NID AND t2.VoterID = 5) INSERT #Times VALUES ('Try 5 with *', DATEDIFF(MILLISECOND, @dt, CURRENT_TIMESTAMP)) SET @i = @i - 1 ENDGOAnd then wrote some code to see the statistics like this-- Show normal statisticsSELECT TryName, COUNT(*) AS [COUNT], MIN(ms) AS [MIN], MAX(ms) AS [MAX], AVG(ms) AS [AVG], STDEV(ms) AS [STDEV]FROM #TimesGROUP BY TryNameORDER BY 1-- Show distributionSELECT ms, SUM(CASE WHEN TryName = 'Try 1 with t2.NID' THEN 1 ELSE 0 END) AS [Try 1 with t2.NID], SUM(CASE WHEN TryName = 'Try 2 with 0' THEN 1 ELSE 0 END) AS [Try 2 with 0], SUM(CASE WHEN TryName = 'Try 3 with 1' THEN 1 ELSE 0 END) AS [Try 3 with 1], SUM(CASE WHEN TryName = 'Try 4 with NULL' THEN 1 ELSE 0 END) AS [Try 4 with NULL], SUM(CASE WHEN TryName = 'Try 5 with *' THEN 1 ELSE 0 END) AS [Try 5 with *]FROM #TimesGROUP BY msORDER BY 1
These are the two outputs I got. You may get other values, but they should be somewhat consistent.BTW, I am running on SQL Server 2005 Developer Edition 9.00.3152.00TryName COUNT MIN MAX AVG STDEV----------------- ----- --- --- --- ----------------Try 1 with t2.NID 100 123 160 138 8.39213195724899Try 2 with 0 100 123 170 140 9.45216559014041Try 3 with 1 100 123 156 138 6.94579300037365Try 4 with NULL 100 123 156 138 6.89806736219163 -- Smallest spread of timeTry 5 with * 100 123 156 141 7.23359167252670ms Try 1 with t2.NID Try 2 with 0 Try 3 with 1 Try 4 with NULL Try 5 with *--- ----------------- ------------ ------------ --------------- ------------123 11 9 9 9 4126 9 6 8 6 3140 58 54 65 64 61143 14 14 15 17 17153 1 3 0 1 3156 6 11 3 3 12160 1 2 0 0 0170 0 1 0 0 0
The second output is interpreted like this- Try 1 has 20% of the total times at the two fastest times
- Try 2 has 15% of the total times at the two fastest times
- Try 3 has 17% of the total times at the two fastest times
- Try 4 has 15% of the total times at the two fastest times
- Try 5 has 7% of the total times at the two fastest times
and- Try 1 has 8% of the total times at the four slowest times
- Try 2 has 17% of the total times at the four slowest times
- Try 3 has 3% of the total times at the four slowest times
- Try 4 has 4% of the total times at the four slowest times
- Try 5 has 15% of the total times at the four slowest times
Peter LarssonHelsingborg, SwedenEDIT: Typo spotted by Rockmoose