Files
sql-server-samples/samples/features/intelligent-query-processing/notebooks/Table_Variable_Deferred_Compilation.ipynb
T

359 KiB

Table Variable Deferred Compilation

Table Variable Deferred Compilation (TVDC) improves plan quality and overall performance for queries that reference table variables. TVDC is a feature under the Intelligent Query Processing suite of features.

This example will show you how upgrading to Database Compatibility Level 150 could improve performance due to Table Variable Deferred Compilation, when your workload references table variables.

More information about this feature is available here.

Step 1: Setup WideWorldImportersDW database

You could choose to use a container to evaluate this feature. Create an instance of SQL Server 2019 using a Docker image and restore the WideWorldImportersDW database backup

You will need the WideWorldImportersDW database for this exercise. If you don't have this sample database, then you download the sample database here.

Restore the copied WideWorldImportersDW database backup into the container and restore the backup.

Docker Commands
docker pull mcr.microsoft.com/mssql/server:2019-latest

docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=`<A Strong Password`>" -p 1445:1433 --name sql2019demo -d mcr.microsoft.com/mssql/server:2019-latest

docker cp ".\Downloads\WideWorldImportersDW-Full.bak" sql2019demo:/var/opt/mssql/data

Note: For Linux installations the default path to use is /var/opt/mssql

In [7]:
USE [master]
GO
IF EXISTS (SELECT [database_id] FROM sys.databases WHERE [name] = 'WideWorldImportersDW')
ALTER DATABASE [WideWorldImportersDW] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO

DECLARE @datafilepath VARCHAR(8000) = CAST(SERVERPROPERTY('InstanceDefaultDataPath') AS VARCHAR(4000)) + 'WideWorldImportersDW.mdf'
DECLARE @logfilepath VARCHAR(8000) = CAST(SERVERPROPERTY('InstanceDefaultLogPath') AS VARCHAR(4000)) + 'WideWorldImportersDW.ldf'
DECLARE @inmemfilepath VARCHAR(8000) = CAST(SERVERPROPERTY('InstanceDefaultDataPath') AS VARCHAR(4000)) + 'WideWorldImportersDW_InMemory_Data_1'
DECLARE @secondaryfilepath VARCHAR(8000) = CAST(SERVERPROPERTY('InstanceDefaultDataPath') AS VARCHAR(4000))+ 'WideWorldImportersDW_2.ndf'

-- Change @backupfile file path as needed
DECLARE @backupfile VARCHAR(8000) = 'E:\SampleDBs\WideWorldImportersDW-Full.bak'
RESTORE DATABASE WideWorldImportersDW
FROM DISK = @backupfile 
WITH MOVE 'WWI_Primary' TO @datafilepath,
    MOVE 'WWI_UserData' TO @secondaryfilepath,
    MOVE 'WWIDW_InMemory_Data_1' TO @inmemfilepath,
    MOVE 'WWI_Log' TO @logfilepath, NOUNLOAD, REPLACE, STATS = 10
GO

USE [master]
GO
ALTER DATABASE [WideWorldImportersDW] MODIFY FILE ( NAME = N'WWI_Log', SIZE = 4GB )
GO

Step 2: Enlarge the WideWorldImportersDW database

In [3]:
USE WideWorldImportersDW;
GO

/*
Assumes a fresh restore of WideWorldImportersDW
*/

IF OBJECT_ID('Fact.OrderHistory') IS NULL 
BEGIN
    SELECT [Order Key], [City Key], [Customer Key], [Stock Item Key], [Order Date Key], [Picked Date Key], [Salesperson Key], [Picker Key], [WWI Order ID], [WWI Backorder ID], Description, Package, Quantity, [Unit Price], [Tax Rate], [Total Excluding Tax], [Tax Amount], [Total Including Tax], [Lineage Key]
    INTO Fact.OrderHistory
    FROM Fact.[Order];
END;

ALTER TABLE Fact.OrderHistory
ADD CONSTRAINT PK_Fact_OrderHistory PRIMARY KEY NONCLUSTERED ([Order Key] ASC, [Order Date Key] ASC) WITH (DATA_COMPRESSION = PAGE);
GO

CREATE INDEX IX_Stock_Item_Key
ON Fact.OrderHistory ([Stock Item Key])
INCLUDE(Quantity)
WITH (DATA_COMPRESSION = PAGE);
GO

CREATE INDEX IX_OrderHistory_Quantity
ON Fact.OrderHistory ([Quantity])
INCLUDE([Order Key])
WITH (DATA_COMPRESSION = PAGE);
GO

CREATE INDEX IX_OrderHistory_CustomerKey
ON Fact.OrderHistory([Customer Key])
INCLUDE ([Total Including Tax])
WITH (DATA_COMPRESSION = PAGE);
GO

IF (SELECT COUNT(*) FROM [Fact].[OrderHistory]) < 3702592
BEGIN
	DECLARE @i smallint
	SET @i = 0
	WHILE @i < 4
	BEGIN
		INSERT INTO [Fact].[OrderHistory] ([City Key], [Customer Key], [Stock Item Key], [Order Date Key], [Picked Date Key], [Salesperson Key], [Picker Key], [WWI Order ID], [WWI Backorder ID], Description, Package, Quantity, [Unit Price], [Tax Rate], [Total Excluding Tax], [Tax Amount], [Total Including Tax], [Lineage Key])
		SELECT [City Key], [Customer Key], [Stock Item Key], [Order Date Key], [Picked Date Key], [Salesperson Key], [Picker Key], [WWI Order ID], [WWI Backorder ID], Description, Package, Quantity, [Unit Price], [Tax Rate], [Total Excluding Tax], [Tax Amount], [Total Including Tax], [Lineage Key]
		FROM [Fact].[OrderHistory];

		SET @i = @i +1
	END;
END
GO

IF OBJECT_ID('Fact.OrderHistoryExtended') IS NULL 
BEGIN
    SELECT [Order Key], [City Key], [Customer Key], [Stock Item Key], [Order Date Key], [Picked Date Key], [Salesperson Key], [Picker Key], [WWI Order ID], [WWI Backorder ID], Description, Package, Quantity, [Unit Price], [Tax Rate], [Total Excluding Tax], [Tax Amount], [Total Including Tax], [Lineage Key]
    INTO Fact.OrderHistoryExtended
    FROM Fact.[OrderHistory];
END;

ALTER TABLE Fact.OrderHistoryExtended
ADD CONSTRAINT PK_Fact_OrderHistoryExtended PRIMARY KEY NONCLUSTERED ([Order Key] ASC, [Order Date Key] ASC)
WITH (DATA_COMPRESSION = PAGE);
GO

CREATE INDEX IX_Stock_Item_Key
ON Fact.OrderHistoryExtended ([Stock Item Key])
INCLUDE (Quantity);
GO

IF (SELECT COUNT(*) FROM [Fact].[OrderHistory]) < 29620736
BEGIN
	DECLARE @i smallint
	SET @i = 0
	WHILE @i < 3
	BEGIN
		INSERT Fact.OrderHistoryExtended([City Key], [Customer Key], [Stock Item Key], [Order Date Key], [Picked Date Key], [Salesperson Key], [Picker Key], [WWI Order ID], [WWI Backorder ID], Description, Package, Quantity, [Unit Price], [Tax Rate], [Total Excluding Tax], [Tax Amount], [Total Including Tax], [Lineage Key])
		SELECT [City Key], [Customer Key], [Stock Item Key], [Order Date Key], [Picked Date Key], [Salesperson Key], [Picker Key], [WWI Order ID], [WWI Backorder ID], Description, Package, Quantity, [Unit Price], [Tax Rate], [Total Excluding Tax], [Tax Amount], [Total Including Tax], [Lineage Key]
		FROM Fact.OrderHistoryExtended;

		SET @i = @i +1
	END;
END
GO

UPDATE Fact.OrderHistoryExtended
SET [WWI Order ID] = [Order Key];
GO

-- Repeat the following until log shrinks. These demos don't require much log space.
CHECKPOINT
GO
DBCC SHRINKFILE (N'WWI_Log' , 0, TRUNCATEONLY)
GO
SELECT * FROM sys.dm_db_log_space_usage
GO

Step 3: Execute the query without Table Variable Deferred Compilation

If the database is configured with a non-default compatibility level scuh as 140 (which maps to SQL Server 2017), then TVDC is disabled.

In [1]:
USE [WideWorldImportersDW];
GO
ALTER DATABASE [WideWorldImportersDW] SET COMPATIBILITY_LEVEL = 140;
GO
ALTER DATABASE SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE;
GO

DECLARE @Order TABLE 
	([Order Key] BIGINT NOT NULL,
	 [Quantity] INT NOT NULL
	);

INSERT @Order
SELECT [Order Key], [Quantity]
FROM [Fact].[OrderHistory]
WHERE  [Quantity] > 99;

SELECT oh.[Order Key], oh.[Order Date Key],
	   oh.[Unit Price], o.Quantity
FROM Fact.OrderHistoryExtended AS oh
INNER JOIN @Order AS o
	ON o.[Order Key] = oh.[Order Key]
WHERE oh.[Unit Price] > 0.10
ORDER BY oh.[Unit Price] DESC;
GO
Out [1]:
Commands completed successfully.
Commands completed successfully.
Commands completed successfully.
(490928 rows affected)
(490928 rows affected)
Displaying Top 500 rows.
Total execution time: 00:00:17.819
Order KeyOrder Date KeyUnit PriceQuantity
7970132015-10-14112.00100
5933272014-02-28112.00100
8045352014-12-05112.00100
9074762013-03-11112.00100
8269122014-05-19112.00100
8346502015-11-16112.00100
6667442013-12-09112.00100
6469382013-09-12112.00100
8949502015-07-18112.00100
8471872015-01-13112.00100
9978652013-07-25112.00100
9978752013-09-19112.00100
9978822013-10-10112.00100
9979722015-07-16112.00100
9673262015-07-18112.00100
11957102014-01-24112.00100
9774522013-04-19112.00100
9774542013-04-19112.00100
9774672013-05-02112.00100
9774702013-05-08112.00100
9776202013-06-14112.00100
13963452015-07-18112.00100
13988552015-12-09112.00100
10206602015-12-18112.00100
15767882014-07-04112.00100
11401802015-01-26112.00100
14766882015-03-10112.00100
13364202015-03-03112.00100
13390032015-08-12112.00100
15363952015-04-22112.00100
15366862015-05-21112.00100
12042982014-11-10112.00100
14463372014-01-29112.00100
14865752014-02-14112.00100
14867222015-01-14112.00100
13264842015-03-16112.00100
18469192014-04-24112.00100
19107872015-08-20112.00100
23760422015-03-16112.00100
20422822015-09-23112.00100
21315432014-03-05112.00100
16873462015-07-29112.00100
21811152014-03-12112.00100
21094702015-08-26112.00100
24235372014-11-10112.00100
24261272015-12-15112.00100
25244492013-03-11112.00100
23434352014-09-25112.00100
23034112013-04-12112.00100
23034182013-04-18112.00100
23835072014-03-05112.00100
28830672014-09-25112.00100
26374572015-08-26112.00100
26452822014-12-05112.00100
26476752013-11-11112.00100
26172012015-05-21112.00100
26958952013-07-20112.00100
29130902016-05-02112.00100
29154272013-03-11112.00100
26855432013-03-11112.00100
27335602013-09-12112.00100
32580432014-02-14112.00100
32190652014-03-12112.00100
32216122015-12-09112.00100
27656662015-04-15112.00100
29664722015-07-10112.00100
29481352015-10-20112.00100
30327922014-07-04112.00100
31045472014-03-05112.00100
31135112015-09-23112.00100
33857952015-12-18112.00100
34255052015-02-25112.00100
34279392013-03-19112.00100
32908542014-12-17112.00100
36438192014-11-10112.00100
1580062015-05-29112.00100
409632013-08-28112.00100
401932013-08-23112.00100
434482013-09-12112.00100
1820112015-09-23112.00100
2761762015-11-14112.00100
2762542015-01-13112.00100
2762582015-01-14112.00100
10657682014-12-10112.00100
13323092015-04-17112.00100
13392192016-02-10112.00100
13223492016-05-02112.00100
13226762015-06-06112.00100
12198282015-01-26112.00100
15194192016-02-11112.00100
16400512015-05-21112.00100
20754972015-12-15112.00100
14823242015-11-14112.00100
18988522014-10-06112.00100
14624892013-11-20112.00100
15693152015-07-10112.00100
17034412015-11-27112.00100
12255532014-02-18112.00100
17432412015-07-16112.00100
19643712014-02-28112.00100
24162122015-03-19112.00100
21945922014-10-06112.00100
36326232013-07-23112.00100
3504042014-12-17112.00100
2897382013-05-02112.00100
2897412013-05-08112.00100
3203152014-12-05112.00100
4020372015-01-26112.00100
4418192015-09-08112.00100
3711192013-11-11112.00100
3712362015-09-26112.00100
4419662015-10-14112.00100
3921082015-11-03112.00100
3714072013-11-20112.00100
4987992014-09-25112.00100
5059172014-10-06112.00100
6060962015-12-09112.00100
5294342014-08-02112.00100
5861972015-04-17112.00100
6094942016-02-20112.00100
18345352015-03-20112.00100
18968302016-02-10112.00100
20600332014-01-24112.00100
18244682015-08-10112.00100
22328752013-07-25112.00100
18855732015-05-26112.00100
22990492015-04-22112.00100
19231392015-09-26112.00100
19232592015-10-06112.00100
19232652015-10-09112.00100
24967482013-11-20112.00100
24998342015-08-15112.00100
24999022015-01-26112.00100
21217902015-03-10112.00100
25369492014-04-24112.00100
21811912014-08-02112.00100
21834892014-04-24112.00100
23192352014-03-24112.00100
29157172013-06-14112.00100
27364302013-12-23112.00100
24695292013-02-11112.00100
26580652015-08-26112.00100
25702672014-06-19112.00100
27793342015-11-11112.00100
25901212014-12-10112.00100
28463402015-11-11112.00100
27889442013-08-23112.00100
27890032015-08-12112.00100
30620852013-09-19112.00100
29355712016-01-04112.00100
34213732015-05-29112.00100
32247352015-09-05112.00100
32842352015-08-10112.00100
28660332015-10-06112.00100
30122132016-02-11112.00100
34411362014-06-19112.00100
29710922014-08-02112.00100
30461212013-06-03112.00100
27118872015-01-13112.00100
30281932016-01-04112.00100
30312182013-07-23112.00100
31070602015-10-14112.00100
35597842014-04-24112.00100
32739672016-01-04112.00100
33511802013-04-18112.00100
34937582015-08-22112.00100
31751162014-05-19112.00100
36241932013-06-03112.00100
36324062013-06-14112.00100
35497302013-06-26112.00100
8201162014-08-18112.00100
7305492015-12-18112.00100
7307902015-11-03112.00100
8676492016-02-10112.00100
7204832013-11-11112.00100
7206182013-11-20112.00100
8003302014-01-24112.00100
9287512016-05-02112.00100
7971002013-07-20112.00100
8272972013-04-19112.00100
8272992013-04-19112.00100
8372252015-04-22112.00100
8303382014-07-04112.00100
9936882015-03-03112.00100
10947142014-01-29112.00100
13590142014-07-07112.00100
10005612014-01-06112.00100
14590692014-03-05112.00100
11206182015-11-16112.00100
11511572015-05-26112.00100
15723982015-11-16112.00100
530822013-11-11112.00100
548772013-11-20112.00100
580342013-12-09112.00100
2020052016-01-04112.00100
2020382016-01-04112.00100
95492013-03-04112.00100
177172013-04-19112.00100
198932013-05-02112.00100
178612013-04-19112.00100
212442013-05-08112.00100
279042013-06-14112.00100
351472013-07-23112.00100
1494222015-04-17112.00100
1268862014-12-24112.00100
3168822014-01-24112.00100
2863892013-04-16112.00100
3651292013-08-23112.00100
3651422013-08-28112.00100
3041002016-03-01112.00100
2838742015-08-26112.00100
4364052013-12-23112.00100
4065902015-03-16112.00100
8650762014-12-17112.00100
5131612015-03-19112.00100
36429512014-02-14112.00100
31358562013-09-19112.00100
33340802015-04-22112.00100
36354662014-04-26112.00100
3849422015-03-19112.00100
6225362015-03-20112.00100
8636282014-01-24112.00100
5221942015-03-03112.00100
5224852015-03-20112.00100
5529592014-04-24112.00100
5825172013-07-25112.00100
9039452013-03-19112.00100
9238192015-09-05112.00100
9140592015-08-28112.00100
9964962015-11-27112.00100
11148472015-12-09112.00100
8835782015-07-16112.00100
10065522016-05-26112.00100
15123042013-11-11112.00100
15153122014-04-24112.00100
14924182015-12-09112.00100
16361072015-08-28112.00100
9662172015-01-13112.00100
13952412015-11-03112.00100
10238702015-02-25112.00100
15755652015-03-07112.00100
11472632013-08-28112.00100
11472502013-08-23112.00100
16159582014-04-11112.00100
12212942015-08-20112.00100
12213052015-08-22112.00100
12214052014-02-14112.00100
12214622014-03-05112.00100
13253882016-02-20112.00100
15056392013-02-11112.00100
15453152015-08-12112.00100
14821962015-11-27112.00100
15856702014-02-26112.00100
14654362015-08-28112.00100
20357802015-09-08112.00100
16930062014-08-18112.00100
16963772014-04-22112.00100
17463352014-06-19112.00100
17463582015-03-07112.00100
18231372015-09-23112.00100
17565402015-08-20112.00100
24826942015-11-14112.00100
25831262015-03-16112.00100
25833082014-11-13112.00100
27423392014-01-24112.00100
27423812015-10-20112.00100
27422462014-01-06112.00100
24725012015-03-16112.00100
29659142014-12-24112.00100
28442362013-06-26112.00100
29284342014-08-20112.00100
29849512013-03-04112.00100
32570102013-06-20112.00100
27722612013-07-23112.00100
32839212013-07-25112.00100
32839312013-09-19112.00100
32839382013-10-10112.00100
26338612014-01-24112.00100
28917542014-07-04112.00100
26542652015-07-16112.00100
34609362014-04-11112.00100
34610432013-03-19112.00100
34611332014-08-18112.00100
30517232015-03-16112.00100
30736412014-03-24112.00100
35429392014-03-12112.00100
33240202016-03-01112.00100
31122182015-08-15112.00100
31122302015-08-27112.00100
34045152015-10-09112.00100
34044492015-10-06112.00100
35627182014-12-17112.00100
33570682014-04-22112.00100
27334332015-04-17112.00100
32243232013-06-26112.00100
27687902015-10-06112.00100
27687962015-10-09112.00100
27688282016-05-11112.00100
32838232014-04-11112.00100
33037762014-12-05112.00100
29478872014-11-10112.00100
31467162013-03-04112.00100
31253372015-08-10112.00100
32411062014-02-18112.00100
32501732015-08-15112.00100
28533802015-05-21112.00100
33307292015-05-21112.00100
31015472015-09-26112.00100
31040382015-06-06112.00100
31065272014-12-17112.00100
31066092015-09-08112.00100
31324972014-12-10112.00100
4284432015-03-10112.00100
4413972015-08-12112.00100
8097052016-01-04112.00100
5988462013-06-20112.00100
8543952015-05-29112.00100
6189642013-06-03112.00100
4983952013-06-20112.00100
8701702015-12-15112.00100
7508562014-01-06112.00100
8769882016-03-01112.00100
5086192013-06-14112.00100
5088052013-07-23112.00100
5156772016-03-01112.00100
7199272013-04-18112.00100
5258092014-04-22112.00100
7201312013-06-26112.00100
7202032015-04-22112.00100
7967662015-09-08112.00100
5833412014-05-19112.00100
5760422014-10-06112.00100
8369552015-05-21112.00100
9597062014-07-07112.00100
8469002015-11-14112.00100
11353622014-09-25112.00100
11355042013-06-03112.00100
16327672016-03-01112.00100
9670112015-01-14112.00100
9693802015-04-22112.00100
10906562013-06-26112.00100
10907892013-11-11112.00100
10910702013-11-20112.00100
9797612015-12-09112.00100
9798012014-07-04112.00100
9771752013-03-04112.00100
9829812016-02-20112.00100
13915492015-12-18112.00100
11114402014-04-26112.00100
10000042015-09-05112.00100
11252662014-03-24112.00100
11506792013-09-12112.00100
15790022013-07-23112.00100
14719762015-10-20112.00100
16169122013-03-11112.00100
15586242015-08-28112.00100
15562302015-09-23112.00100
15588472014-03-05112.00100
10434312014-02-28112.00100
12361272014-11-10112.00100
12778622013-12-23112.00100
12932672014-11-13112.00100
12194722014-11-13112.00100
14989062014-09-12112.00100
15022392015-11-11112.00100
15219242015-08-26112.00100
20751492014-02-14112.00100
17975892015-08-28112.00100
13616392013-07-20112.00100
14085772015-09-26112.00100
14283022015-02-02112.00100
18342442015-03-03112.00100
18366212015-05-26112.00100
19128802015-04-17112.00100
19160752015-08-10112.00100
19161962016-05-26112.00100
14863162016-01-04112.00100
19224622015-08-15112.00100
19224742015-08-27112.00100
16025512013-02-11112.00100
20596062016-02-20112.00100
20126062015-04-17112.00100
17193652014-12-24112.00100
17370312014-02-26112.00100
18615622015-11-16112.00100
13062162014-11-10112.00100
13462662015-09-08112.00100
19639402014-02-14112.00100
24132242013-07-23112.00100
24358052016-02-20112.00100
17848682013-04-16112.00100
22350462015-03-03112.00100
18500172014-12-24112.00100
22586682015-10-14112.00100
24022592015-03-10112.00100
19275822014-11-13112.00100
21279752014-03-12112.00100
21280272014-02-18112.00100
24624062014-01-29112.00100
22817062013-09-19112.00100
22817132013-10-10112.00100
22818502013-09-19112.00100
23221802015-07-10112.00100
23621222014-02-14112.00100
24526362013-07-20112.00100
22518092014-11-10112.00100
23721702015-04-15112.00100
34647042015-10-20112.00100
34052542013-11-06112.00100
34079312015-07-16112.00100
33881112013-07-25112.00100
35832792015-05-29112.00100
35857752013-07-25112.00100
35658412014-02-26112.00100
35661042015-07-16112.00100
33536702013-11-11112.00100
34761062015-03-19112.00100
35295332016-01-04112.00100
35339642015-03-16112.00100
35164872015-07-10112.00100
36436062016-05-26112.00100
36206342013-11-06112.00100
35959352015-03-07112.00100
36171242015-05-26112.00100
36171882013-06-20112.00100
36782032015-03-07112.00100
1926582015-11-16112.00100
658852014-01-24112.00100
864302014-05-19112.00100
29022302015-12-18112.00100
28289882014-12-05112.00100
32012682015-01-26112.00100
32474462015-08-27112.00100
33753412015-03-03112.00100
36434132014-02-28112.00100
36595252015-12-15112.00100
2272092016-05-11112.00100
2250262016-05-02112.00100
165082013-04-12112.00100
175902013-04-18112.00100
299782013-06-26112.00100
1570802015-05-26112.00100
525012013-11-06112.00100
1307792015-01-14112.00100
1305622015-01-13112.00100
1504212015-04-22112.00100
2090602016-02-10112.00100
2127982016-03-01112.00100
67212013-02-11112.00100
1983442015-12-15112.00100
625362014-01-06112.00100
2960742014-09-12112.00100
3363282014-12-24112.00100
2860832014-04-22112.00100
2833592015-08-10112.00100
2894692014-07-04112.00100
3706942015-08-15112.00100
3707062015-08-27112.00100
16167562013-03-04112.00100
15965822015-03-16112.00100
14861842013-06-14112.00100
15660882013-03-04112.00100
15661302013-08-28112.00100
15661172013-08-23112.00100
18105532013-06-26112.00100
22526472015-12-09112.00100
23354202015-11-03112.00100
22953122013-04-12112.00100
22986752013-06-26112.00100
23789842015-08-12112.00100
20420862013-07-20112.00100
25942482015-08-28112.00100
24592302015-03-03112.00100
16893202015-09-23112.00100
25339192015-10-06112.00100
25339252015-10-09112.00100
25340092014-04-26112.00100
25340902014-01-06112.00100
25367062015-12-09112.00100
21045602015-08-10112.00100
18301592015-03-16112.00100
22250132016-05-02112.00100
18693802016-05-02112.00100
23588742015-09-05112.00100
24257542016-01-04112.00100
29186182013-07-23112.00100
25262662015-09-26112.00100
29774452015-02-02112.00100
26775262015-08-28112.00100
24032832015-10-14112.00100
28242462015-01-26112.00100
23029812015-09-23112.00100
23055712013-09-12112.00100
24691982014-01-06112.00100
26347072015-07-18112.00100
21706982014-04-24112.00100
26169512014-01-29112.00100
26200872014-09-25112.00100
26202202013-09-12112.00100
26201732015-02-02112.00100

Observe the query execution plan (or actual plan).

TVDC_Disabled_Plan

Notice the actual vs. estimated rows for the @Order Table Scan. Only one row estimated vs. 490k rows actual rows, which represents a sizeable misestimation.

Note: Table Variables only exist at runtime, which means that SQL Server cannot properly estimate the number of rows for these objects. And so, the legacy behavior for lower compatibility levels is to estimate only one row for Table Variables.

In turn, this misestimation propagates up the plan, namely impacting the join algorithm chosen (Nested Loops) which are more efficient for smaller input tables.

Step 4: Execute the query by removing any hint restriction

Run the same query from Step 3, but now with the default compatibility level (150), allowing SQL Server to use Table Variable Deferred Compilation.

In [2]:
USE [WideWorldImportersDW];
GO
ALTER DATABASE [WideWorldImportersDW] SET COMPATIBILITY_LEVEL = 150;
GO
ALTER DATABASE SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE;
GO

DECLARE @Order TABLE 
	([Order Key] BIGINT NOT NULL,
	 [Quantity] INT NOT NULL
	);

INSERT @Order
SELECT [Order Key], [Quantity]
FROM [Fact].[OrderHistory]
WHERE  [Quantity] > 99;

SELECT oh.[Order Key], oh.[Order Date Key],
	   oh.[Unit Price], o.Quantity
FROM Fact.OrderHistoryExtended AS oh
INNER JOIN @Order AS o
	ON o.[Order Key] = oh.[Order Key]
WHERE oh.[Unit Price] > 0.10
ORDER BY oh.[Unit Price] DESC;
GO
Out [2]:
Commands completed successfully.
Commands completed successfully.
Commands completed successfully.
(490928 rows affected)
(490928 rows affected)
Displaying Top 500 rows.
Total execution time: 00:00:08.024
Order KeyOrder Date KeyUnit PriceQuantity
8039652015-09-23112.00100
35661042015-07-16112.00100
15790022013-07-23112.00100
9992612014-08-18112.00100
32642782014-02-28112.00100
15219242015-08-26112.00100
1409372015-03-07112.00100
28371732015-02-02112.00100
1709182015-07-29112.00100
27452922015-12-18112.00100
8312182013-04-16112.00100
8835782015-07-16112.00100
13376652014-05-19112.00100
2972732015-12-09112.00100
729382014-03-05112.00100
25053302014-03-05112.00100
15363952015-04-22112.00100
14473492014-08-18112.00100
14473022014-04-11112.00100
9992362014-04-24112.00100
3712362015-09-26112.00100
31977232014-04-26112.00100
10948392014-08-20112.00100
279042013-06-14112.00100
19053142015-03-20112.00100
8555742015-08-28112.00100
18301592015-03-16112.00100
25064562015-07-16112.00100
25191612013-12-23112.00100
23029812015-09-23112.00100
34367722014-01-29112.00100
18693802016-05-02112.00100
23034112013-04-12112.00100
10950462014-04-22112.00100
10626772014-12-05112.00100
14470612014-04-26112.00100
16873462015-07-29112.00100
24651902015-07-29112.00100
20768572014-03-24112.00100
4174812013-04-12112.00100
36773242013-06-14112.00100
32624582015-11-14112.00100
23065772013-03-19112.00100
23050442013-11-20112.00100
18277762014-03-12112.00100
31089352015-12-15112.00100
23055712013-09-12112.00100
15588472014-03-05112.00100
36429512014-02-14112.00100
14441432013-12-23112.00100
15229462013-04-12112.00100
6419372013-08-23112.00100
23027112013-11-11112.00100
16217742016-02-20112.00100
7201312013-06-26112.00100
34369662013-11-20112.00100
20422822015-09-23112.00100
10005612014-01-06112.00100
28097852014-12-17112.00100
20754972015-12-15112.00100
14447002015-08-15112.00100
14447122015-08-27112.00100
23284012014-03-24112.00100
7199272013-04-18112.00100
22464512015-03-10112.00100
31087492014-12-17112.00100
3968522015-04-15112.00100
23034182013-04-18112.00100
20751492014-02-14112.00100
6419502013-08-28112.00100
7202032015-04-22112.00100
15586242015-08-28112.00100
19048382015-03-03112.00100
2845492013-09-19112.00100
34568222013-04-19112.00100
10000042015-09-05112.00100
2845562013-10-10112.00100
30776042015-09-23112.00100
30731972015-03-16112.00100
10322382013-12-23112.00100
34021662015-03-16112.00100
758842014-03-24112.00100
8269122014-05-19112.00100
1490792015-04-15112.00100
3711192013-11-11112.00100
36782032015-03-07112.00100
16327672016-03-01112.00100
7204832013-11-11112.00100
110242013-03-11112.00100
15796612013-05-08112.00100
29849512013-03-04112.00100
13383362015-11-11112.00100
14463372014-01-29112.00100
15194192016-02-11112.00100
35627182014-12-17112.00100
31055012015-03-07112.00100
15796582013-05-02112.00100
34560842014-08-20112.00100
18292082015-02-02112.00100
36438192014-11-10112.00100
32005062013-03-11112.00100
2616982013-06-20112.00100
31070602015-10-14112.00100
2664612015-04-17112.00100
36436062016-05-26112.00100
5258092014-04-22112.00100
10311742015-03-19112.00100
1983442015-12-15112.00100
25209962014-04-11112.00100
18105532013-06-26112.00100
3203152014-12-05112.00100
13590142014-07-07112.00100
36952412015-03-16112.00100
26338612014-01-24112.00100
31066092015-09-08112.00100
31065272014-12-17112.00100
2874502016-01-04112.00100
31062582013-12-23112.00100
20420862013-07-20112.00100
7322152015-02-25112.00100
15359312013-06-26112.00100
15366862015-05-21112.00100
30736412014-03-24112.00100
34894922014-11-13112.00100
31973832015-07-18112.00100
22419352014-05-19112.00100
16893202015-09-23112.00100
7324492015-07-10112.00100
30793532015-09-23112.00100
30113532014-03-12112.00100
13632562015-10-14112.00100
31059642015-08-12112.00100
19037792015-04-15112.00100
351472013-07-23112.00100
26334862015-11-14112.00100
36434132014-02-28112.00100
25434572014-07-07112.00100
36206342013-11-06112.00100
7529332015-09-05112.00100
25038382015-03-20112.00100
23044152015-12-09112.00100
34636762015-10-20112.00100
29828162014-02-26112.00100
31881402015-12-18112.00100
30122132016-02-11112.00100
8346502015-11-16112.00100
35658412014-02-26112.00100
16224712016-05-02112.00100
16313352014-04-22112.00100
14467382014-08-20112.00100
4174882013-04-18112.00100
34637182015-08-27112.00100
34568202013-04-19112.00100
8543952015-05-29112.00100
255552013-06-03112.00100
3956592013-11-06112.00100
32247352015-09-05112.00100
29614042015-02-25112.00100
3905422013-06-03112.00100
17844162016-01-04112.00100
20126062015-04-17112.00100
14766882015-03-10112.00100
14567682013-11-11112.00100
15453152015-08-12112.00100
29664722015-07-10112.00100
22054842014-04-26112.00100
8636282014-01-24112.00100
95492013-03-04112.00100
32012682015-01-26112.00100
5756662015-08-10112.00100
16246802014-05-19112.00100
15922442015-09-05112.00100
13116832014-03-05112.00100
4110142015-07-16112.00100
14576222013-03-19112.00100
28646982013-03-04112.00100
14770242015-11-14112.00100
15457142015-10-20112.00100
14831882015-10-14112.00100
178612013-04-19112.00100
23760422015-03-16112.00100
177172013-04-19112.00100
198932013-05-02112.00100
14770582015-05-26112.00100
23938702016-05-02112.00100
25339252015-10-09112.00100
212442013-05-08112.00100
25339192015-10-06112.00100
13264842015-03-16112.00100
36992942015-08-12112.00100
22052302015-08-12112.00100
34741702015-07-29112.00100
20135922015-05-21112.00100
14534982015-12-15112.00100
12623092013-08-28112.00100
29154272013-03-11112.00100
13081982013-11-06112.00100
33809532015-08-28112.00100
14536362015-06-06112.00100
2833592015-08-10112.00100
36595252015-12-15112.00100
25340902014-01-06112.00100
24069432016-05-26112.00100
14823242015-11-14112.00100
11638572015-03-19112.00100
25901212014-12-10112.00100
4741922016-05-11112.00100
14828662013-07-20112.00100
21279752014-03-12112.00100
21280272014-02-18112.00100
34052542013-11-06112.00100
15450592016-01-04112.00100
25340092014-04-26112.00100
18766562013-07-20112.00100
22791072015-04-17112.00100
12194722014-11-13112.00100
32243232013-06-26112.00100
17867002015-07-16112.00100
30042452014-01-24112.00100
8369552015-05-21112.00100
32221722016-02-11112.00100
15693152015-07-10112.00100
34045152015-10-09112.00100
32216122015-12-09112.00100
35429392014-03-12112.00100
34044492015-10-06112.00100
3698742015-07-18112.00100
11773682014-06-19112.00100
18128352015-08-27112.00100
18127942015-08-15112.00100
32739672016-01-04112.00100
11333492015-09-23112.00100
3117302014-08-20112.00100
3116922014-01-29112.00100
17848682013-04-16112.00100
14174772014-06-19112.00100
15123042013-11-11112.00100
35832792015-05-29112.00100
27248762013-08-23112.00100
21852252013-04-19112.00100
28977952015-08-22112.00100
27248892013-08-28112.00100
21852272013-04-19112.00100
7422552014-02-26112.00100
28977842015-08-20112.00100
15134992013-06-20112.00100
22818502013-09-19112.00100
8590982015-12-09112.00100
35544032015-12-15112.00100
6611862016-02-11112.00100
28967902016-02-20112.00100
22817132013-10-10112.00100
35456952015-12-15112.00100
31159772015-10-06112.00100
7593332013-04-16112.00100
30858892013-11-20112.00100
31812272014-04-22112.00100
21834892014-04-24112.00100
8949502015-07-18112.00100
22817062013-09-19112.00100
24032832015-10-14112.00100
11344682015-08-28112.00100
32041402014-08-02112.00100
4939672013-08-23112.00100
31162572015-10-09112.00100
12204302015-11-27112.00100
21315432014-03-05112.00100
20132982014-03-05112.00100
8661952014-03-12112.00100
15300472013-04-18112.00100
4939802013-08-28112.00100
8994672013-02-11112.00100
34749842014-12-10112.00100
34754032015-09-08112.00100
24047902013-07-20112.00100
8238052014-04-11112.00100
23738762014-04-26112.00100
12198282015-01-26112.00100
666822014-01-29112.00100
25912552015-05-29112.00100
1950952015-11-27112.00100
25655992015-05-26112.00100
14843512015-08-22112.00100
11340652015-04-15112.00100
32720392015-08-15112.00100
16681382015-07-10112.00100
35554712015-06-06112.00100
32724082015-11-27112.00100
14843402015-08-20112.00100
22815432013-07-25112.00100
34761062015-03-19112.00100
8650762014-12-17112.00100
21847692013-03-04112.00100
19537702014-08-18112.00100
13941592015-01-14112.00100
21309092014-04-22112.00100
35530542013-11-11112.00100
13952412015-11-03112.00100
32722022015-08-27112.00100
35530632013-11-20112.00100
14544622015-11-16112.00100
34815262013-03-04112.00100
25659652015-11-03112.00100
35857752013-07-25112.00100
4284432015-03-10112.00100
3446912014-03-12112.00100
5131612015-03-19112.00100
25495122014-08-02112.00100
31050172015-04-15112.00100
14861842013-06-14112.00100
19171822015-08-26112.00100
3087132014-08-18112.00100
2884762014-08-02112.00100
25235462014-07-04112.00100
13316342015-12-09112.00100
31045472014-03-05112.00100
3805052015-04-22112.00100
25522062015-09-05112.00100
5125472016-02-20112.00100
36418902015-01-26112.00100
24725012015-03-16112.00100
19167422015-01-26112.00100
24721382016-05-26112.00100
19161962016-05-26112.00100
22497172015-04-17112.00100
7961652015-08-12112.00100
23116752014-12-05112.00100
24967482013-11-20112.00100
3802712015-11-16112.00100
8796392015-04-15112.00100
31040382015-06-06112.00100
25258612014-10-06112.00100
22203942015-10-14112.00100
12620822013-08-23112.00100
19160752015-08-10112.00100
36183752014-04-24112.00100
25109972016-05-02112.00100
13957762015-03-16112.00100
14797252013-06-03112.00100
15153122014-04-24112.00100
25486182013-11-06112.00100
3981442015-03-07112.00100
22473492014-12-05112.00100
808912014-04-22112.00100
36166022013-10-10112.00100
7598792013-02-11112.00100
10097242013-02-11112.00100
16007022013-04-16112.00100
5410402014-04-26112.00100
2934672014-04-26112.00100
22490802014-08-02112.00100
34279392013-03-19112.00100
8022312015-08-22112.00100
30659532015-12-18112.00100
22986752013-06-26112.00100
13963452015-07-18112.00100
19569822014-01-29112.00100
9693802015-04-22112.00100
19825142015-12-18112.00100
8097052016-01-04112.00100
25244492013-03-11112.00100
31547512015-03-19112.00100
34503562013-05-08112.00100
25507062015-11-16112.00100
34503532013-05-02112.00100
9693012016-01-04112.00100
34503432014-01-29112.00100
22990492015-04-22112.00100
36128122015-03-20112.00100
4429512015-12-15112.00100
31554172015-07-18112.00100
14873622015-10-20112.00100
30665612014-02-26112.00100
3954292015-12-18112.00100
7173502013-12-09112.00100
22993572015-12-18112.00100
15145052015-08-20112.00100
15145162015-08-22112.00100
31444852014-02-26112.00100
36122462015-03-03112.00100
14867222015-01-14112.00100
14865752014-02-14112.00100
35276202014-04-22112.00100
18469192014-04-24112.00100
9451752016-05-11112.00100
9691642016-01-04112.00100
12796962014-09-25112.00100
26031332015-08-28112.00100
9001922013-11-11112.00100
14863162016-01-04112.00100
30098502014-12-10112.00100
21758022016-05-11112.00100
12027302015-07-16112.00100
29863812015-08-10112.00100
22180262015-09-08112.00100
34305292015-02-25112.00100
24945132015-08-22112.00100
29890172015-08-26112.00100
13282862014-09-12112.00100
14590692014-03-05112.00100
30920912014-08-02112.00100
28070342015-12-09112.00100
9189502014-11-10112.00100
4983952013-06-20112.00100
33321552015-10-14112.00100
30923032015-05-21112.00100
4987992014-09-25112.00100
14597862014-08-02112.00100
30924472015-03-16112.00100
24949002015-08-26112.00100
31015472015-09-26112.00100
14601572015-09-26112.00100
13226762015-06-06112.00100
4963932015-11-11112.00100
18817662015-11-11112.00100
6094942016-02-20112.00100
22350462015-03-03112.00100
22503332014-12-10112.00100
24695292013-02-11112.00100
14803942015-01-13112.00100
29867502013-03-11112.00100
20676582015-05-21112.00100
5951722014-09-12112.00100
24691982014-01-06112.00100
24934562015-08-10112.00100
29870882015-09-26112.00100
31022752014-08-18112.00100
33570682014-04-22112.00100
2336602016-05-11112.00100
9182482013-06-14112.00100
34647042015-10-20112.00100
4405652015-06-06112.00100
24958872016-05-02112.00100
2911092014-11-10112.00100
21156672013-07-25112.00100
10116482013-06-20112.00100
10118132014-02-14112.00100
9184652013-07-23112.00100
1427022015-03-16112.00100
8627082015-07-29112.00100
23017342015-11-03112.00100
31027792015-08-28112.00100
1826282015-09-26112.00100
3113912015-03-03112.00100
27118872015-01-13112.00100
13223492016-05-02112.00100
1844642015-10-06112.00100
14010302015-08-27112.00100
25262662015-09-26112.00100
8676492016-02-10112.00100
1852812015-10-09112.00100
34297222013-11-11112.00100
8677752015-12-18112.00100
5909172016-05-26112.00100
36650842016-03-01112.00100
13299962013-07-25112.00100
7400822015-03-07112.00100
18252462013-07-25112.00100
10430072014-06-19112.00100
30312182013-07-23112.00100
33881112013-07-25112.00100
36331082014-11-10112.00100
29769972013-03-11112.00100
10712782015-08-28112.00100
32775972014-08-20112.00100
21811152014-03-12112.00100
10434312014-02-28112.00100
1917602015-11-11112.00100
36654332015-03-07112.00100
13207172014-07-07112.00100
30301022014-09-25112.00100
10440852015-03-10112.00100
21224162013-09-19112.00100
29774452015-02-02112.00100
16658972014-12-17112.00100
289392013-06-20112.00100
21224232013-10-10112.00100
13105142014-12-17112.00100
7925672013-12-23112.00100
18258572014-02-18112.00100
28463402015-11-11112.00100
32794112015-11-03112.00100
33894432013-06-03112.00100
32796632014-04-22112.00100
30197242015-12-09112.00100
1504212015-04-22112.00100
18249832015-08-26112.00100
21238372015-07-16112.00100
7537322013-07-25112.00100
3927512015-07-29112.00100
9852312016-02-10112.00100
27227922014-09-25112.00100
35151372013-04-12112.00100
580342013-12-09112.00100
13930812015-01-13112.00100
9856662016-03-01112.00100
33908752015-03-19112.00100
7537422013-09-19112.00100
7537492013-10-10112.00100
30178982014-08-20112.00100

Observe the query execution plan (or actual plan).

TVDC_Enabled_Plan

Notice the actual vs. estimated rows for the @Order Table Scan now. 490k estimated vs. 490k rows actual rows. This is because the Atble Variable was materialized during compilation, and the actual cardinality was used to cerry on optimizing the query plan.

In turn, this improved estimation propagated up the plan, namely the chosen join algorithm (Hash Match) which are efficient for larger input tables. Also note that given the correct estimations, SQL Server determined that the query plan should be executed with parallelism.

As you can see from the execution times, the query with Table Variable Deferred Compilation finished much faster! From ~18s down to ~8s.