Skip to content

Commit

Permalink
cleanup for script for IQP
Browse files Browse the repository at this point in the history
  • Loading branch information
rgward committed Jul 5, 2022
1 parent 5801a6b commit 07200e3
Show file tree
Hide file tree
Showing 15 changed files with 47 additions and 47 deletions.
6 changes: 3 additions & 3 deletions demos/sqlserver2022/IQP/persistedmgf/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ The following is a demonstration of memory grant feedback persistence in SQL Ser
3. In order to run some of the examples, you will need larger tables than what is installed by default in WideWorldImportersDW. Therefore, run the script **extendwwidw.sql** to create a larger table.
4. Setup the demo by executing the script **setup.sql**
5. Simulate statistics out of date by executing the script **set_stats.sql**.
6. Using SSMS choose the button to see Include Actual Execution plan and execute a query that will use a memory grant from execute_query.sql. The query will take about 30 seconds to complete.
6. Using SSMS choose the button to see Include Actual Execution plan and execute a query that will use a memory grant from **execute_query.sql**. The query will take about 30 seconds to complete.
7. Select the Execution Plan in the results. You will see a graphical showplan. Notice the yellow warning on the hash join operator. If you hover over this operator with the cursor you will see a warning about a spill to tempdb. Notice the spill involves writing out ~400Mb of pages to tempdb. You can also see the estimated number of rows is far lower than the actual number of rows.
8. Hover over the SELECT operator and notice the memory grant is 1.4Mb which is far short of the ~400Mb spilled to tempdb. Right click the SELECT operator and select Properties. Expand the MemoryGrantInfo option. Notice the **IsMemoryGrantFeedbackAdjusted** = NoFirstExecution. This means no adjustment has been made since there is no feedback and they query was just compiled. The "used" memory is only the memory used as part of the grant and does not account for the spill.
9. Now execute **get_plan_feedback.sql** and notice the output. You will see that feedback has been stored to allocate a significant larger memory grant on next query execution.
10. Run **execute_query.sql** again
11. This time the query runs in seconds. Notice there is no spill warning for the hash join. Hovering over the SELECT operator will show a significantly larger grant. Right clicking on the SELECT operator and selecting properties will show in the MemoryGrantInfo section **IsMemoryGrantFeedbackAdjusted** = YesAdjusting.
11. This time the query runs in seconds. Notice there is no spill warning for the hash join. Hovering over the SELECT operator will show a significantly larger grant. Right clicking on the SELECT operator and selecting properties will show in the MemoryGrantInfo section **IsMemoryGrantFeedbackAdjusted = YesAdjusting**.
12. Run **get_plan_feedback.sql** again and see the last_query_memory_kb reflect the new larger memory grant.
13. Execute the script **clear_proc_cache.sql**. This will clear the plan cache. Prior to SQL Server 2022, this would have "lost" the memory grant feedback.
13. Execute the script **clear_proc_cache.sql**. This will clear the plan cache. Prior to SQL Se****rver 2022, this would have "lost" the memory grant feedback.
14. Run **execute_query.sql** again. You will see the grant is still using the feedback now in the query store and runs in a few seconds.
11 changes: 0 additions & 11 deletions demos/sqlserver2022/IQP/psp/proc.sql

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,47 @@ 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
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
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
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
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/pspopt/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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-- The best plan for this parameter is an index scan
USE WideWorldImporters;
GO
ALTER DATABASE SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE;
GO
-- The best plan for this parameter is an index scan
EXEC Warehouse.GetStockItemsbySupplier 4;
GO
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
USE WideWorldImporters;
GO
-- The best plan for this parameter is an index seek
SET STATISTICS TIME ON;
GO
USE WideWorldImporters;
GO
-- The best plan for this parameter is an index seek
EXEC Warehouse.GetStockItemsbySupplier 2
GO
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ MOVE 'WWI_UserData' TO 'f:\data\WideWorldImporters_UserData.ndf',
MOVE 'WWI_Log' TO 'g:\log\WideWorldImporters.ldf',
MOVE 'WWI_InMemory_Data_1' TO 'f:\data\WideWorldImporters_InMemory_Data_1',
stats=5;
go
GO
File renamed without changes.

0 comments on commit 07200e3

Please sign in to comment.