Skip to content

Commit ebdce8e

Browse files
authored
Add files via upload
1 parent 0de898e commit ebdce8e

File tree

4 files changed

+165
-0
lines changed

4 files changed

+165
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
-- Param Sniffing with Hash Spill
2+
3+
-- Setup
4+
--USE AdventureWorks2014
5+
USE AdventureWorks2016CTP3
6+
GO
7+
DROP TABLE CustomersState
8+
GO
9+
CREATE TABLE CustomersState (CustomerID int PRIMARY KEY, [Address] CHAR(200), [State] CHAR(2))
10+
GO
11+
INSERT INTO CustomersState (CustomerID, [Address])
12+
SELECT CustomerID, 'Address' FROM Sales.Customer
13+
GO
14+
UPDATE CustomersState SET [State] = 'NY' WHERE CustomerID % 100 <> 1
15+
UPDATE CustomersState SET [State] = 'WA' WHERE CustomerID % 100 = 1
16+
GO
17+
18+
UPDATE STATISTICS CustomersState WITH FULLSCAN
19+
GO
20+
21+
CREATE PROCEDURE CustomersByState @State CHAR(2) AS
22+
BEGIN
23+
DECLARE @CustomerID int
24+
SELECT @CustomerID = e.CustomerID FROM Sales.Customer e
25+
INNER JOIN CustomersState es ON e.CustomerID = es.CustomerID
26+
WHERE es.[State] = @State
27+
OPTION (MAXDOP 1)
28+
END
29+
GO
30+
31+
-- Get Actual Execution Plan
32+
33+
-- Execute the stored procedure first with parameter value ‘WA’ – which will select 1% of data.
34+
DBCC FREEPROCCACHE
35+
GO
36+
EXEC CustomersByState 'WA'
37+
GO
38+
39+
EXEC CustomersByState 'NY'
40+
GO
41+
42+
/*
43+
Observe the type of Spill = Recursion
44+
Occurs when the build input does not fit into available memory,
45+
resulting in the split of input into multiple partitions that are processed separately.
46+
47+
If any of these partitions still do not fit into available memory,
48+
it is split into sub-partitions, which are also processed separately.
49+
This splitting process continues until each partition fits into available memory
50+
or until the maximum recursion level is reached.
51+
In this case it stopped at level 1.
52+
*/
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
-- Mem Grant Warning
2+
3+
-- Added MIN_GRANT_PERCENT for repro on SQL 2014 SP2 and 2016 only, because fix for this scenario is in those releases.
4+
5+
--Execute in 2014 for warning; coming soon for 2016
6+
USE [memgrants]
7+
GO
8+
DBCC FREEPROCCACHE
9+
GO
10+
SELECT o.col3, o.col2, d.col2
11+
FROM orders o
12+
JOIN orders_detail d ON o.col2 = d.col1
13+
WHERE o.col3 <= 8000
14+
OPTION (LOOP JOIN, MAXDOP 1, MIN_GRANT_PERCENT = 20)
15+
GO
16+
17+
/*
18+
In SELECT node properties:
19+
MaxQueryMemory for maximum query memory grant under RG MAX_MEMORY_PERCENT hint
20+
MaxCompileMemory for maximum query optimizer memory in KB during compile under RG
21+
*/
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
USE [master]
2+
GO
3+
IF NOT EXISTS (SELECT name FROM sys.databases WHERE name = N'memgrants')
4+
CREATE DATABASE [memgrants]
5+
GO
6+
7+
USE [memgrants]
8+
GO
9+
10+
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[orders]') AND type in (N'U'))
11+
BEGIN
12+
CREATE TABLE [dbo].[orders](
13+
[col1] [int] NOT NULL,
14+
[col2] [int] NULL,
15+
[col3] [int] NULL,
16+
PRIMARY KEY CLUSTERED
17+
(
18+
[col1] ASC
19+
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
20+
) ON [PRIMARY]
21+
END
22+
GO
23+
24+
SET NOCOUNT ON
25+
GO
26+
DECLARE @x int
27+
SET @x = 0
28+
WHILE (@x < 10000000)
29+
BEGIN
30+
INSERT INTO [dbo].[orders] VALUES (@x, @x, @x)
31+
SET @x = @x + 1
32+
END
33+
GO
34+
35+
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[orders_detail]') AND type in (N'U'))
36+
BEGIN
37+
CREATE TABLE [dbo].[orders_detail](
38+
[col1] [int] NULL,
39+
[col2] [int] NULL,
40+
[col3] [char](5000) NOT NULL
41+
) ON [PRIMARY]
42+
END
43+
GO
44+
45+
DECLARE @x int
46+
DECLARE @y int
47+
SET @x = 0
48+
SET @y = 1
49+
WHILE (@x < 10000)
50+
BEGIN
51+
INSERT INTO [dbo].[orders_detail] VALUES (@x, @y, 'x')
52+
IF ((@y % 100) = 0)
53+
BEGIN
54+
SET @y = 1
55+
SET @x = @x + 1
56+
END
57+
SET @y = @y + 1
58+
END
59+
GO
60+
61+
SET NOCOUNT OFF
62+
GO
63+
64+
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[orders_detail]') AND name = N'od_cl_idx')
65+
CREATE UNIQUE CLUSTERED INDEX [od_cl_idx] ON [dbo].[orders_detail]
66+
(
67+
[col1] ASC,
68+
[col2] ASC
69+
)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]
70+
GO
71+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
-- Sort Spill
2+
3+
-- Get Actual Execution Plan
4+
5+
USE AdventureWorks2014
6+
--USE AdventureWorks2016CTP3
7+
GO
8+
--Execute
9+
DBCC FREEPROCCACHE
10+
GO
11+
SELECT *
12+
FROM Sales.SalesOrderDetail SOD
13+
INNER JOIN Production.Product P ON SOD.ProductID = P.ProductID
14+
ORDER BY Style
15+
OPTION (QUERYTRACEON 9481)
16+
GO
17+
18+
/*
19+
Observe the type of Spill = 1
20+
Means one pass over the data was enough to complete the sort in the Worktable
21+
*/

0 commit comments

Comments
 (0)