From e59d108b8339b13be972606ff7ff40c676dcdedb Mon Sep 17 00:00:00 2001 From: pmasl Date: Mon, 11 Mar 2019 15:28:39 -0700 Subject: [PATCH] Adding IQP demos cc/ josackmsft --- ...P Demos Enlarging WideWorldImportersDW.sql | 103 ++++++++++++++++++ ...Public Preview - APPROX_COUNT_DISTINCT.sql | 23 ++++ ...ublic Preview - Batch Mode on Rowstore.sql | 48 ++++++++ ...ortersDW Public Preview - Row Mode MGF.sql | 43 ++++++++ ...WorldImportersDW Public Preview - TVDC.sql | 70 ++++++++++++ .../intelligent-query-processing/readme.md | 16 +++ 6 files changed, 303 insertions(+) create mode 100644 samples/features/intelligent-query-processing/Intelligent QP Demos Enlarging WideWorldImportersDW.sql create mode 100644 samples/features/intelligent-query-processing/Intelligent QP Demos WideWorldImportersDW Public Preview - APPROX_COUNT_DISTINCT.sql create mode 100644 samples/features/intelligent-query-processing/Intelligent QP Demos WideWorldImportersDW Public Preview - Batch Mode on Rowstore.sql create mode 100644 samples/features/intelligent-query-processing/Intelligent QP Demos WideWorldImportersDW Public Preview - Row Mode MGF.sql create mode 100644 samples/features/intelligent-query-processing/Intelligent QP Demos WideWorldImportersDW Public Preview - TVDC.sql create mode 100644 samples/features/intelligent-query-processing/readme.md diff --git a/samples/features/intelligent-query-processing/Intelligent QP Demos Enlarging WideWorldImportersDW.sql b/samples/features/intelligent-query-processing/Intelligent QP Demos Enlarging WideWorldImportersDW.sql new file mode 100644 index 00000000..9fd76a49 --- /dev/null +++ b/samples/features/intelligent-query-processing/Intelligent QP Demos Enlarging WideWorldImportersDW.sql @@ -0,0 +1,103 @@ +-- ***************************************************** -- +-- Purpose of this script: make WideWorldImportersDW +-- bigger - so you can see more impactful +-- Intelligent QP demonstrations (aka.ms/iqp) +-- +-- Script last updated 10/02/2018 +-- +-- Database backup source: aka.ms/wwibak +-- +-- Initial database file to restore before beginning this script: +-- WideWorldImportersDW-Full.bak +-- ***************************************************** -- + +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 + +/* + Reality check... Starting count should be 231,412 +*/ +SELECT COUNT(*) FROM Fact.OrderHistory; +GO + +/* + Make this table bigger (exec as desired) + Notice the "GO 4" +*/ +INSERT 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; +GO 4 + +/* + Should be 3,702,592 +*/ +SELECT COUNT(*) FROM Fact.OrderHistory; +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 + +/* + Should be 3,702,592 +*/ +SELECT COUNT(*) FROM Fact.OrderHistoryExtended; +GO + +/* + Make this table bigger (exec as desired) + Notice the "GO 3" +*/ +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; +GO 3 + +/* + Should be 29,620,736 +*/ +SELECT COUNT(*) FROM Fact.OrderHistoryExtended; +GO + +UPDATE Fact.OrderHistoryExtended +SET [WWI Order ID] = [Order Key]; +GO diff --git a/samples/features/intelligent-query-processing/Intelligent QP Demos WideWorldImportersDW Public Preview - APPROX_COUNT_DISTINCT.sql b/samples/features/intelligent-query-processing/Intelligent QP Demos WideWorldImportersDW Public Preview - APPROX_COUNT_DISTINCT.sql new file mode 100644 index 00000000..26e6012c --- /dev/null +++ b/samples/features/intelligent-query-processing/Intelligent QP Demos WideWorldImportersDW Public Preview - APPROX_COUNT_DISTINCT.sql @@ -0,0 +1,23 @@ +-- ******************************************************** -- +-- Approximate count distinct + +-- See https://aka.ms/IQP for more background + +-- Demo scripts: https://aka.ms/IQPDemos + +-- Demo uses SQL Server 2019 Public Preview and also works on Azure SQL DB + +-- Email IntelligentQP@microsoft.com for questions\feedback +-- ******************************************************** -- +USE WideWorldImportersDW; +GO + +-- Compare execution time and distinct counts +SELECT COUNT(DISTINCT [WWI Order ID]) +FROM [Fact].[OrderHistoryExtended] +OPTION (USE HINT('DISALLOW_BATCH_MODE'), RECOMPILE); -- Isolating out BMOR + +SELECT APPROX_COUNT_DISTINCT([WWI Order ID]) +FROM [Fact].[OrderHistoryExtended] +OPTION (USE HINT('DISALLOW_BATCH_MODE'), RECOMPILE); -- Isolating out BMOR + diff --git a/samples/features/intelligent-query-processing/Intelligent QP Demos WideWorldImportersDW Public Preview - Batch Mode on Rowstore.sql b/samples/features/intelligent-query-processing/Intelligent QP Demos WideWorldImportersDW Public Preview - Batch Mode on Rowstore.sql new file mode 100644 index 00000000..39dea339 --- /dev/null +++ b/samples/features/intelligent-query-processing/Intelligent QP Demos WideWorldImportersDW Public Preview - Batch Mode on Rowstore.sql @@ -0,0 +1,48 @@ +-- ******************************************************** -- +-- Batch mode on rowstore + +-- See https://aka.ms/IQP for more background + +-- Demo scripts: https://aka.ms/IQPDemos + +-- This demo is on SQL Server 2019 Public Preview and coming soon to Azure SQL DB + +-- Email IntelligentQP@microsoft.com for questions\feedback +-- ******************************************************** -- + +ALTER DATABASE [WideWorldImportersDW] SET COMPATIBILITY_LEVEL = 150; +GO + +ALTER DATABASE SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE; +GO + +-- Row mode due to hint +SELECT [Tax Rate], + [Lineage Key], + SUM([Quantity]) AS SUM_QTY, + SUM([Unit Price]) AS SUM_BASE_PRICE, + COUNT(*) AS COUNT_ORDER +FROM [Fact].[OrderHistoryExtended] +WHERE [Order Date Key] <= dateadd(dd, -73, '2015-11-13') +GROUP BY [Tax Rate], + [Lineage Key] +ORDER BY [Tax Rate], + [Lineage Key] +OPTION (RECOMPILE, USE HINT('DISALLOW_BATCH_MODE')); + +-- Batch mode on rowstore eligible +SELECT [Tax Rate], + [Lineage Key], + [Salesperson Key], + SUM([Quantity]) AS SUM_QTY, + SUM([Unit Price]) AS SUM_BASE_PRICE, + COUNT(*) AS COUNT_ORDER +FROM [Fact].[OrderHistoryExtended] +WHERE [Order Date Key] <= dateadd(dd, -73, '2015-11-13') +GROUP BY [Tax Rate], + [Lineage Key], + [Salesperson Key] +ORDER BY [Tax Rate], + [Lineage Key], + [Salesperson Key] +OPTION (RECOMPILE); \ No newline at end of file diff --git a/samples/features/intelligent-query-processing/Intelligent QP Demos WideWorldImportersDW Public Preview - Row Mode MGF.sql b/samples/features/intelligent-query-processing/Intelligent QP Demos WideWorldImportersDW Public Preview - Row Mode MGF.sql new file mode 100644 index 00000000..419b96c4 --- /dev/null +++ b/samples/features/intelligent-query-processing/Intelligent QP Demos WideWorldImportersDW Public Preview - Row Mode MGF.sql @@ -0,0 +1,43 @@ +-- ******************************************************** -- +-- Row mode memory grant feedback + +-- See https://aka.ms/IQP for more background +-- Demo scripts: https://aka.ms/IQPDemos + +-- This demo is on SQL Server 2019 Public Preview and works in Azure SQL DB too +-- SSMS v17.9 or higher + +-- Email IntelligentQP@microsoft.com for questions\feedback +-- ******************************************************** -- + +ALTER DATABASE WideWorldImportersDW SET COMPATIBILITY_LEVEL = 150; +GO + +ALTER DATABASE SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE; +GO + +USE WideWorldImportersDW; +GO + +-- Simulate out-of-date stats +UPDATE STATISTICS Fact.OrderHistory +WITH ROWCOUNT = 1; +GO + +-- Include actual execution plan +-- Execute once to see spills (row mode) +-- Execute a second time to see correction +SELECT + fo.[Order Key], fo.Description, + si.[Lead Time Days] +FROM Fact.OrderHistory AS fo +INNER HASH JOIN Dimension.[Stock Item] AS si + ON fo.[Stock Item Key] = si.[Stock Item Key] +WHERE fo.[Lineage Key] = 9 + AND si.[Lead Time Days] > 19; + +-- Cleanup +UPDATE STATISTICS Fact.OrderHistory +WITH ROWCOUNT = 3702672; +GO + diff --git a/samples/features/intelligent-query-processing/Intelligent QP Demos WideWorldImportersDW Public Preview - TVDC.sql b/samples/features/intelligent-query-processing/Intelligent QP Demos WideWorldImportersDW Public Preview - TVDC.sql new file mode 100644 index 00000000..3b1dd936 --- /dev/null +++ b/samples/features/intelligent-query-processing/Intelligent QP Demos WideWorldImportersDW Public Preview - TVDC.sql @@ -0,0 +1,70 @@ +-- ******************************************************** -- +-- Table variable deferred compilation + +-- See https://aka.ms/IQP for more background + +-- Demo scripts: https://aka.ms/IQPDemos + +-- This demo is on SQL Server 2019 Public Preview and works in Azure SQL DB too + +-- Email IntelligentQP@microsoft.com for questions\feedback +-- ******************************************************** -- + +USE [master]; +GO + +ALTER DATABASE [WideWorldImportersDW] SET COMPATIBILITY_LEVEL = 140; +GO + +USE [WideWorldImportersDW]; +GO + +DECLARE @Order TABLE + ([Order Key] BIGINT NOT NULL, + [Quantity] INT NOT NULL + ); + +INSERT @Order +SELECT [Order Key], [Quantity] +FROM [Fact].[OrderHistory] +WHERE [Quantity] > 1; + +-- Look at estimated rows, speed, join algorithm +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 + +USE [master] +GO + +ALTER DATABASE [WideWorldImportersDW] SET COMPATIBILITY_LEVEL = 150 +GO + +USE [WideWorldImportersDW] +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; + +-- Look at estimated rows, speed, join algorithm +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 + diff --git a/samples/features/intelligent-query-processing/readme.md b/samples/features/intelligent-query-processing/readme.md new file mode 100644 index 00000000..dd1da160 --- /dev/null +++ b/samples/features/intelligent-query-processing/readme.md @@ -0,0 +1,16 @@ +# Intelligent Query Processing Demos + +Here are the instructions to prepare for demonstrating Intelligent QP's latest round of features: + +1) Download WideWorldImportersDW-Full.bak from https://aka.ms/wwidwbak + +2) Enlarge the database using the following script: https://aka.ms/wwidwenlarge + +Demos (with more on the way!): + +- [Row mode memory grant feedback](Intelligent%20QP%20Demos%20WideWorldImportersDW%20Public%20Preview%20-%20Row%20Mode%20MGF.sql) +- [Batch mode on rowstore](Intelligent%20QP%20Demos%20WideWorldImportersDW%20Public%20Preview%20-%20Batch%20Mode%20on%20Rowstore.sql) +- [APPROX_COUNT_DISTINCT](Intelligent%20QP%20Demos%20WideWorldImportersDW%20Public%20Preview%20-%20APPROX_COUNT_DISTINCT.sql) +- [Table variable deferred compilation](Intelligent%20QP%20Demos%20WideWorldImportersDW%20Public%20Preview%20-%20TVDC.sql) + +Email IntelligentQP@microsoft.com if questions in the meantime. \ No newline at end of file