From ebdce8ea5c5ad06575491735e7748a0d206e7a97 Mon Sep 17 00:00:00 2001 From: Pedro Lopes Date: Thu, 15 Sep 2016 16:01:49 -0700 Subject: [PATCH] Add files via upload --- .../demos/mssqltiger/Showplan/HashWarning.sql | 52 ++++++++++++++ .../Showplan/MemoryGrant_Warning.sql | 21 ++++++ .../Showplan/MemoryGrant_Warning_Setup.sql | 71 +++++++++++++++++++ .../demos/mssqltiger/Showplan/SortWarning.sql | 21 ++++++ 4 files changed, 165 insertions(+) create mode 100644 samples/demos/mssqltiger/Showplan/HashWarning.sql create mode 100644 samples/demos/mssqltiger/Showplan/MemoryGrant_Warning.sql create mode 100644 samples/demos/mssqltiger/Showplan/MemoryGrant_Warning_Setup.sql create mode 100644 samples/demos/mssqltiger/Showplan/SortWarning.sql diff --git a/samples/demos/mssqltiger/Showplan/HashWarning.sql b/samples/demos/mssqltiger/Showplan/HashWarning.sql new file mode 100644 index 00000000..3457e005 --- /dev/null +++ b/samples/demos/mssqltiger/Showplan/HashWarning.sql @@ -0,0 +1,52 @@ +-- Param Sniffing with Hash Spill + +-- Setup +--USE AdventureWorks2014 +USE AdventureWorks2016CTP3 +GO +DROP TABLE CustomersState +GO +CREATE TABLE CustomersState (CustomerID int PRIMARY KEY, [Address] CHAR(200), [State] CHAR(2)) +GO +INSERT INTO CustomersState (CustomerID, [Address]) +SELECT CustomerID, 'Address' FROM Sales.Customer +GO +UPDATE CustomersState SET [State] = 'NY' WHERE CustomerID % 100 <> 1 +UPDATE CustomersState SET [State] = 'WA' WHERE CustomerID % 100 = 1 +GO + +UPDATE STATISTICS CustomersState WITH FULLSCAN +GO + +CREATE PROCEDURE CustomersByState @State CHAR(2) AS +BEGIN + DECLARE @CustomerID int + SELECT @CustomerID = e.CustomerID FROM Sales.Customer e + INNER JOIN CustomersState es ON e.CustomerID = es.CustomerID + WHERE es.[State] = @State + OPTION (MAXDOP 1) +END +GO + +-- Get Actual Execution Plan + +-- Execute the stored procedure first with parameter value ‘WA’ – which will select 1% of data. +DBCC FREEPROCCACHE +GO +EXEC CustomersByState 'WA' +GO + +EXEC CustomersByState 'NY' +GO + +/* +Observe the type of Spill = Recursion +Occurs when the build input does not fit into available memory, +resulting in the split of input into multiple partitions that are processed separately. + +If any of these partitions still do not fit into available memory, +it is split into sub-partitions, which are also processed separately. +This splitting process continues until each partition fits into available memory +or until the maximum recursion level is reached. +In this case it stopped at level 1. +*/ \ No newline at end of file diff --git a/samples/demos/mssqltiger/Showplan/MemoryGrant_Warning.sql b/samples/demos/mssqltiger/Showplan/MemoryGrant_Warning.sql new file mode 100644 index 00000000..7c0a2853 --- /dev/null +++ b/samples/demos/mssqltiger/Showplan/MemoryGrant_Warning.sql @@ -0,0 +1,21 @@ +-- Mem Grant Warning + +-- Added MIN_GRANT_PERCENT for repro on SQL 2014 SP2 and 2016 only, because fix for this scenario is in those releases. + +--Execute in 2014 for warning; coming soon for 2016 +USE [memgrants] +GO +DBCC FREEPROCCACHE +GO +SELECT o.col3, o.col2, d.col2 +FROM orders o +JOIN orders_detail d ON o.col2 = d.col1 +WHERE o.col3 <= 8000 +OPTION (LOOP JOIN, MAXDOP 1, MIN_GRANT_PERCENT = 20) +GO + +/* +In SELECT node properties: +MaxQueryMemory for maximum query memory grant under RG MAX_MEMORY_PERCENT hint +MaxCompileMemory for maximum query optimizer memory in KB during compile under RG +*/ \ No newline at end of file diff --git a/samples/demos/mssqltiger/Showplan/MemoryGrant_Warning_Setup.sql b/samples/demos/mssqltiger/Showplan/MemoryGrant_Warning_Setup.sql new file mode 100644 index 00000000..62ee3677 --- /dev/null +++ b/samples/demos/mssqltiger/Showplan/MemoryGrant_Warning_Setup.sql @@ -0,0 +1,71 @@ +USE [master] +GO +IF NOT EXISTS (SELECT name FROM sys.databases WHERE name = N'memgrants') +CREATE DATABASE [memgrants] +GO + +USE [memgrants] +GO + +IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[orders]') AND type in (N'U')) +BEGIN +CREATE TABLE [dbo].[orders]( + [col1] [int] NOT NULL, + [col2] [int] NULL, + [col3] [int] NULL, +PRIMARY KEY CLUSTERED +( + [col1] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] +END +GO + +SET NOCOUNT ON +GO +DECLARE @x int +SET @x = 0 +WHILE (@x < 10000000) +BEGIN + INSERT INTO [dbo].[orders] VALUES (@x, @x, @x) + SET @x = @x + 1 +END +GO + +IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[orders_detail]') AND type in (N'U')) +BEGIN +CREATE TABLE [dbo].[orders_detail]( + [col1] [int] NULL, + [col2] [int] NULL, + [col3] [char](5000) NOT NULL +) ON [PRIMARY] +END +GO + +DECLARE @x int +DECLARE @y int +SET @x = 0 +SET @y = 1 +WHILE (@x < 10000) +BEGIN + INSERT INTO [dbo].[orders_detail] VALUES (@x, @y, 'x') + IF ((@y % 100) = 0) + BEGIN + SET @y = 1 + SET @x = @x + 1 + END + SET @y = @y + 1 +END +GO + +SET NOCOUNT OFF +GO + +IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[orders_detail]') AND name = N'od_cl_idx') +CREATE UNIQUE CLUSTERED INDEX [od_cl_idx] ON [dbo].[orders_detail] +( + [col1] ASC, + [col2] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +GO + diff --git a/samples/demos/mssqltiger/Showplan/SortWarning.sql b/samples/demos/mssqltiger/Showplan/SortWarning.sql new file mode 100644 index 00000000..b16479da --- /dev/null +++ b/samples/demos/mssqltiger/Showplan/SortWarning.sql @@ -0,0 +1,21 @@ +-- Sort Spill + +-- Get Actual Execution Plan + +USE AdventureWorks2014 +--USE AdventureWorks2016CTP3 +GO +--Execute +DBCC FREEPROCCACHE +GO +SELECT * +FROM Sales.SalesOrderDetail SOD +INNER JOIN Production.Product P ON SOD.ProductID = P.ProductID +ORDER BY Style +OPTION (QUERYTRACEON 9481) +GO + +/* +Observe the type of Spill = 1 +Means one pass over the data was enough to complete the sort in the Worktable +*/ \ No newline at end of file