Skip to content

Commit

Permalink
Added new demos for SQL Server 2022
Browse files Browse the repository at this point in the history
  • Loading branch information
rgward committed May 11, 2022
1 parent 608aa83 commit b401b6b
Show file tree
Hide file tree
Showing 27 changed files with 297 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
sp_configure 'show advanced', 1;
go
reconfigure;
go
sp_configure 'max degree of parallelism', 0;
go
reconfigure;
go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
SELECT qsp.query_plan_hash, avg_duration/1000 as avg_duration_ms,
avg_cpu_time/1000 as avg_cpu_ms, last_dop, min_dop, max_dop
FROM sys.query_store_runtime_stats qsrs
JOIN sys.query_store_plan qsp
ON qsrs.plan_id = qsp.plan_id
and qsp.query_plan_hash = CONVERT(varbinary(8), cast(4128150668158729174 as bigint))
ORDER by qsrs.last_execution_time;
GO
15 changes: 15 additions & 0 deletions demos/sqlserver2022/IQP/dopfeedback/dopfeedback/dopfeedback.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
USE WideWorldImporters
GO
-- Make sure QS is on and set runtime collection lower than default
ALTER DATABASE WideWorldImporters SET QUERY_STORE = ON
GO
ALTER DATABASE WideWorldImporters SET QUERY_STORE (OPERATION_MODE = READ_WRITE, DATA_FLUSH_INTERVAL_SECONDS = 60, INTERVAL_LENGTH_MINUTES = 1, QUERY_CAPTURE_MODE = ALL)
GO
ALTER DATABASE WideWorldImporters SET QUERY_STORE CLEAR ALL
GO
-- Required as of CTP 2.0
ALTER DATABASE SCOPED CONFIGURATION SET DOP_FEEDBACK = ON
GO
-- Clear proc cache to start with new plans
ALTER DATABASE SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE
GO
20 changes: 20 additions & 0 deletions demos/sqlserver2022/IQP/dopfeedback/dopfeedback/dopxe.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--Setup XE capture
IF EXISTS (SELECT * FROM sys.server_event_sessions WHERE name = 'DOPFeedback')
DROP EVENT SESSION [DOPFeedback] ON SERVER;
GO
CREATE EVENT SESSION [DOPFeedback] ON SERVER
ADD EVENT sqlserver.dop_feedback_eligible_query(
ACTION(sqlserver.query_hash_signed,sqlserver.query_plan_hash_signed,sqlserver.sql_text)),
ADD EVENT sqlserver.dop_feedback_provided(
ACTION(sqlserver.query_hash_signed,sqlserver.query_plan_hash_signed,sqlserver.sql_text)),
ADD EVENT sqlserver.dop_feedback_validation(
ACTION(sqlserver.query_hash_signed,sqlserver.query_plan_hash_signed,sqlserver.sql_text)),
ADD EVENT sqlserver.dop_feedback_reverted(
ACTION(sqlserver.query_hash_signed,sqlserver.query_plan_hash_signed,sqlserver.sql_text))
WITH (MAX_MEMORY=4096 KB,EVENT_RETENTION_MODE=NO_EVENT_LOSS,MAX_DISPATCH_LATENCY=1 SECONDS,MAX_EVENT_SIZE=0 KB,MEMORY_PARTITION_MODE=NONE,TRACK_CAUSALITY=OFF,STARTUP_STATE=OFF)
GO

-- Start XE
ALTER EVENT SESSION [DOPFeedback] ON SERVER
STATE = START;
GO
Binary file not shown.
26 changes: 26 additions & 0 deletions demos/sqlserver2022/IQP/dopfeedback/dopfeedback/populatedata.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
USE WideWorldImporters;
GO
-- Add StockItems to cause a data skew in Suppliers
--
DECLARE @StockItemID int
DECLARE @StockItemName varchar(100)
DECLARE @SupplierID int
SELECT @StockItemID = 228
SET @StockItemName = 'Dallas Cowboys Shirt'+convert(varchar(10), @StockItemID)
SET @SupplierID = 4
DELETE FROM Warehouse.StockItems WHERE StockItemID >= @StockItemID
SET NOCOUNT ON
BEGIN TRANSACTION
WHILE @StockItemID <= 20000000
BEGIN
INSERT INTO Warehouse.StockItems
(StockItemID, StockItemName, SupplierID, UnitPackageID, OuterPackageID, LeadTimeDays,
QuantityPerOuter, IsChillerStock, TaxRate, UnitPrice, TypicalWeightPerUnit, LastEditedBy
)
VALUES (@StockItemID, @StockItemName, @SupplierID, 10, 9, 12, 100, 0, 15.00, 100.00, 0.300, 1)
SET @StockItemID = @StockItemID + 1
SET @StockItemName = 'Dallas Cowboys Shirt'+convert(varchar(10), @StockItemID)
END
COMMIT TRANSACTION
SET NOCOUNT OFF
GO
11 changes: 11 additions & 0 deletions demos/sqlserver2022/IQP/dopfeedback/dopfeedback/proc.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
USE WideWorldImporters;
GO
CREATE OR ALTER PROCEDURE [Warehouse].[GetStockItemsbySupplier] @SupplierID int
AS
BEGIN
SELECT StockItemID, SupplierID, StockItemName, TaxRate, LeadTimeDays
FROM Warehouse.StockItems s
WHERE SupplierID = @SupplierID
ORDER BY StockItemName
END;
GO
25 changes: 25 additions & 0 deletions demos/sqlserver2022/IQP/dopfeedback/dopfeedback/readme.md.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# DOP Feedback in SQL Server 2022

This demo will show you how to see how to get consistent performance with less CPU resources for queries that require parallel operators

## Pre-requisites

- VM or computer with 8 CPUs and at least 24Gb RAM
- SQL Server 2022 CTP 2.0

## Steps

1. Execute configmaxdop.sql
2. Copy the WideWorldImporters sample database from https://github.com/Microsoft/sql-server-samples/releases/download/wide-world-importers-v1.0/WideWorldImporters-Full.bak to a local directory (The restore script assumes c:\sql_sample_databases)
3. Edit the restorewwi.sql script for the correct paths for the backup and where data and log files should go.
4. Execute the script restorewwi.sql
5. Extend the database by executing populatedata.sql. This will take ~13mins to execute. Because of the large transaction the log will grow to ~30Gb and the user FG will grow to about ~6.5Gb
6. Execute dopfeedback.sql to set QDS settings and db setting for DOP feeback (required for CTP 2.0)
7. Execute proc.sql to create a stored procedure
8. Execute dopexec.sql to create an XEvent session.
9. Use SSMS to Watch the XE session to see Live Data
10. Run workload_index_scan_users.cmd
11. Observe the XEvent data. It will take about 10mins to see XXXX event which means the final DOP setting to achieve stability.
12. Cancel the workload from the cmd script
13. Run dop_query_stats.sql to see the changes in DOP and resulting stats. Not the small decrease in avg duration and decrease in needed CPU
14. Use Top Resource Consuming Queries report and look at Avg Duration and Avg CPU to see the steady decrease until stable.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER INDEX FK_Warehouse_StockItems_SupplierID ON Warehouse.StockItems REBUILD;
GO
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
restore database WideWorldImporters from disk = 'c:\sql_sample_databases\WideWorldImporters-Full.bak' with
move 'WWI_Primary' to 'c:\sql_sample_databases\WideWorldImporters.mdf',
move 'WWI_UserData' to 'c:\sql_sample_databases\WideWorldImporters_UserData.ndf',
move 'WWI_Log' to 'c:\sql_sample_databases\WideWorldImporters.ldf',
move 'WWI_InMemory_Data_1' to 'c:\sql_sample_databases\WideWorldImporters_InMemory_Data_1',
stats=5
go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.\ostress -E -Q"EXEC Warehouse.GetStockItemsbySupplier 4;" -n1 -r200 -q -oworkload_wwi_regress -dWideWorldImporters

8 changes: 8 additions & 0 deletions demos/sqlserver2022/IQP/psp/dbcompat160.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
USE WideWorldImporters;
GO
ALTER DATABASE CURRENT SET COMPATIBILITY_LEVEL = 160;
GO
ALTER DATABASE SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE;
GO
ALTER DATABASE CURRENT SET QUERY_STORE CLEAR;
GO
Binary file added demos/sqlserver2022/IQP/psp/ostress.exe
Binary file not shown.
48 changes: 48 additions & 0 deletions demos/sqlserver2022/IQP/psp/populatedata.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
USE WideWorldImporters;
GO
-- Add StockItems to cause a data skew in Suppliers
--
DECLARE @StockItemID int
DECLARE @StockItemName varchar(100)
DECLARE @SupplierID int
SELECT @StockItemID = 228
SET @StockItemName = 'Dallas Cowboys Shirt'+convert(varchar(10), @StockItemID)
SET @SupplierID = 4
DELETE FROM Warehouse.StockItems WHERE StockItemID >= @StockItemID
SET NOCOUNT ON
BEGIN TRANSACTION
WHILE @StockItemID <= 4000000
BEGIN
INSERT INTO Warehouse.StockItems
(StockItemID, StockItemName, SupplierID, UnitPackageID, OuterPackageID, LeadTimeDays,
QuantityPerOuter, IsChillerStock, TaxRate, UnitPrice, TypicalWeightPerUnit, LastEditedBy
)
VALUES (@StockItemID, @StockItemName, @SupplierID, 10, 9, 12, 100, 0, 15.00, 100.00, 0.300, 1)
SET @StockItemID = @StockItemID + 1
SET @StockItemName = 'Dallas Cowboys Shirt'+convert(varchar(10), @StockItemID)
END
COMMIT TRANSACTION
SET NOCOUNT OFF
GO
DECLARE @StockItemID int
DECLARE @StockItemName varchar(100)
DECLARE @SupplierID int
SELECT @StockItemID = 4000001
SET @StockItemName = 'Dallas Cowboys Mug'+convert(varchar(10), @StockItemID)
SET @SupplierID = 5
DELETE FROM Warehouse.StockItems WHERE StockItemID >= @StockItemID
SET NOCOUNT ON
BEGIN TRANSACTION
WHILE @StockItemID <= 8000000
BEGIN
INSERT INTO Warehouse.StockItems
(StockItemID, StockItemName, SupplierID, UnitPackageID, OuterPackageID, LeadTimeDays,
QuantityPerOuter, IsChillerStock, TaxRate, UnitPrice, TypicalWeightPerUnit, LastEditedBy
)
VALUES (@StockItemID, @StockItemName, @SupplierID, 10, 9, 12, 100, 0, 15.00, 100.00, 0.300, 1)
SET @StockItemID = @StockItemID + 1
SET @StockItemName = 'Dallas Cowboys Mug'+convert(varchar(10), @StockItemID)
END
COMMIT TRANSACTION
SET NOCOUNT OFF
GO
11 changes: 11 additions & 0 deletions demos/sqlserver2022/IQP/psp/proc.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
USE WideWorldImporters;
GO
CREATE OR ALTER PROCEDURE [Warehouse].[GetStockItemsbySupplier] @SupplierID int
AS
BEGIN
SELECT StockItemID, SupplierID, StockItemName, TaxRate, LeadTimeDays
FROM Warehouse.StockItems s
WHERE SupplierID = @SupplierID
ORDER BY StockItemName
END;
GO
7 changes: 7 additions & 0 deletions demos/sqlserver2022/IQP/psp/query_plan_scan.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- The best plan for this parameter is an index scan
USE WideWorldImporters;
GO
ALTER DATABASE SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE
GO
EXEC Warehouse.GetStockItemsbySupplier 4;
GO
7 changes: 7 additions & 0 deletions demos/sqlserver2022/IQP/psp/query_plan_seek.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- The best plan for this parameter is an index seek
SET STATISTICS TIME ON;
GO
USE WideWorldImporters;
GO
EXEC Warehouse.GetStockItemsbySupplier 2
GO
8 changes: 8 additions & 0 deletions demos/sqlserver2022/IQP/psp/query_store_dmvs.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
SELECT * FROM sys.query_store_query_text;
GO
SELECT * FROM sys.query_store_query;
GO
SELECT * FROM sys.query_store_plan;
GO
SELECT * FROM sys.query_store_query_variant;
GO
47 changes: 47 additions & 0 deletions demos/sqlserver2022/IQP/psp/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Parameter Sensitive Plan Optimization (PSP) in SQL Server 2022

Here are the steps to demonstrate the new PSP optimization feature for SQL Server 2022

## Prerequisites

- VM with at least 4 CPUs and X Gb RAM
- SQL Server 2022 CTP 2.0
- SQL Server Management Studio (SSMS) Version 19 Preview
- Download ostress.exe from https://www.microsoft.com/en-us/download/details.aspx?id=103126

Follow these steps to demonstrate Parameter Sensitive Plan (PSP) optimization

## Setup the demo

1. Copy WideWorldImporters from https://github.com/Microsoft/sql-server-samples/releases/download/wide-world-importers-v1.0/WideWorldImporters-Full.bak (the restore script assumes c:\sql_sample_databases)
2. Restore the WideWorldImporters backup. You can edit and use the **restorewwi.sql** script.
3. Load and execute the **populatedata.sql** script to load more data into the Warehouse.StockItems table. This script will take 5 mins to run
4. Rebuild an index associated with the table with **rebuild_index.sql**
5. Create a new procedure to be used for the workload test using **proc.sql**.
6. Edit your servername in sqlsetup.cmd and execute the script. This will ensure the WideWorldImporters database is at dbcompat 150 and clear the query store.

## See a PSP problem for a single query execution.

7. Set the actual execution plan option in SSMS. Run query_plan_seek.sql in a query window in SSMS. Note the query time is fast and the values from SET STATISTICS TIME is around 20ms. Note the query plan uses an Index Seek.
8. In a different query window set the actual execution option in SSMS. Run query_plan_scan.sql in a query windows in SSMS. Note the query plan uses an Clustered Index Scan and parallelism.
9. Now go back and run query_plan_seek.sql again. Note the timing from SET STATISTICS IO is now ~250ms. Ten times slower but a single execution seems fast.

## See a workload problem for PSP

10. Setup perfmon to catpure % processor time and batch requests/second
11. Edit the scripts workload_index_scan.cmd and workload_index_seek.cmd for your servername.
12. Put ostress.exe in your path or copy it to the local directory. It is installed by default in C:\Program Files\Microsoft Corporation\RMLUtils
13. Run workload_index_seek.cmd. This should complete in a few seconds. Observe perfmon counters.
14. Run workload_index_scan.cmd. This should take longer but now locks into cache a plan for a scan.
15. Run workload_index_seek.cmd again. Observe perfmon counters. Notice much higher CPU and much lower batch requests/sec.
16. Hit <Ctrl>+<C> in the command window for workload_index_seek.cmd as it can take minutes to complete.
17. Use the query suppliercount.sql to see the skew in supplierID values in the table. This explains why "one size does not fit all" for the stored procedure based on parameter values.

## Solve the problem in SQL Server 2022

17. Let's get this workload much faster using PSP optimization. Execute the T-SQL script **dbcompat160.sql** with SSMS.
18. Run workload_index_seek.cmd again. Should finish in a few seconds.
19. Run workload_index_scan.cmd again.
20. Run workload_index_seek.cmd again and see that it now finishs again in a few seconds. Observe perfmon counters and see consistent performance.
21. Run Top Resource Consuming Queries report and see that there are two plans for the same stored procedure
22. It looks like are "two" queries but these are two query "variants". Use the script query_store_dmvs.sql to see the details.
2 changes: 2 additions & 0 deletions demos/sqlserver2022/IQP/psp/rebuild_index.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER INDEX FK_Warehouse_StockItems_SupplierID ON Warehouse.StockItems REBUILD;
GO
7 changes: 7 additions & 0 deletions demos/sqlserver2022/IQP/psp/restorewwi.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
restore database WideWorldImporters from disk = 'c:\sql_sample_databases\WideWorldImporters-Full.bak' with
move 'WWI_Primary' to 'c:\sql_sample_databases\WideWorldImporters.mdf',
move 'WWI_UserData' to 'c:\sql_sample_databases\WideWorldImporters_UserData.ndf',
move 'WWI_Log' to 'c:\sql_sample_databases\WideWorldImporters.ldf',
move 'WWI_InMemory_Data_1' to 'c:\sql_sample_databases\WideWorldImporters_InMemory_Data_1',
stats=5
go
1 change: 1 addition & 0 deletions demos/sqlserver2022/IQP/psp/sqlsetup.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.\ostress -E -Q"alter database current set compatibility_level = 150; alter database current set query_store clear;DBCC TRACEON (11091, 12619, -1);" -dWideWorldImporters -q
6 changes: 6 additions & 0 deletions demos/sqlserver2022/IQP/psp/suppliercount.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
USE WideWorldImporters;
GO
SELECT SupplierID, count(*) as supplier_count
FROM Warehouse.StockItems
GROUP BY SupplierID;
GO
3 changes: 3 additions & 0 deletions demos/sqlserver2022/IQP/psp/workload_index_scan.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.\ostress -E -Q"ALTER DATABASE SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE;" -n1 -r1 -q -oworkload_wwi_regress -dWideWorldImporters
.\ostress -E -Q"EXEC Warehouse.GetStockItemsbySupplier 4;" -n1 -r1 -q -oworkload_wwi_regress -dWideWorldImporters

1 change: 1 addition & 0 deletions demos/sqlserver2022/IQP/psp/workload_index_seek.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.\ostress -E -Q"EXEC Warehouse.GetStockItemsbySupplier 2;" -n25 -r1000 -q -dWideWorldImporters
11 changes: 11 additions & 0 deletions demos/sqlserver2022/IQP/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# SQL Server 2022 IQP demos

These are demos to be showcase new IQP features in SQL Server 2022.

*psp*

Demo for Parameter Sensitive Plan (PSP) optimization

*dopfeedback*

Demo for Degree of Parallelism feedback
6 changes: 6 additions & 0 deletions demos/sqlserver2022/readme.md
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
# SQL Server 2022 demos

These are demos to be showcase new features in SQL Server 2022.

*IQP*

Demos for the nextgen of Intelligent Query Processing

0 comments on commit b401b6b

Please sign in to comment.