mirror of
https://github.com/Microsoft/sql-server-samples.git
synced 2025-12-08 14:58:54 +00:00
Add files via upload
This commit is contained in:
@@ -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.
|
||||
*/
|
||||
@@ -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
|
||||
*/
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
Reference in New Issue
Block a user