Skip to content

Commit 0c303e1

Browse files
Merge pull request microsoft#335 from JocaPC/master
Integrated FLGP demo into WWI database
2 parents a8ce04d + a52d12d commit 0c303e1

File tree

8 files changed

+80
-27
lines changed

8 files changed

+80
-27
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bin/*
2+
obj/*

samples/databases/wide-world-importers/wwi-ssdt/wwi-ssdt/PostDeploymentScripts/Script.PostDeployment1.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ EXEC DataLoadSimulation.DailyProcessToCreateHistory
171171
@AreDatesPrinted = 1;
172172
GO
173173

174+
:r .\pds400-ins-unkown-orderline.sql
174175

175176
/*
176177
There is one other stored procedure you may find useful:

samples/databases/wide-world-importers/wwi-ssdt/wwi-ssdt/PostDeploymentScripts/pds200-ins-warehouse-packagetypes.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ DECLARE @EndOfTime datetime2(7) = '99991231 23:59:59.9999999'
77
INSERT Warehouse.PackageTypes
88
(PackageTypeID, PackageTypeName, LastEditedBy, ValidFrom, ValidTo)
99
VALUES
10-
(1,'Bag', 1, @CurrentDateTime, @EndOfTime)
10+
(0,'Unknown', 1, @CurrentDateTime, @EndOfTime)
11+
, (1,'Bag', 1, @CurrentDateTime, @EndOfTime)
1112
, (2,'Block', 1, @CurrentDateTime, @EndOfTime)
1213
, (3,'Bottle', 1, @CurrentDateTime, @EndOfTime)
1314
, (4,'Box', 1, @CurrentDateTime, @EndOfTime)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-- Inserting one OrderLine in "Unknown" package needed to demonstrate parameter sniffing and FORCE LAST GOOD PLAN.
2+
PRINT 'Inserting single OrderLine in "Unknown" package.'
3+
GO
4+
5+
BEGIN TRAN;
6+
7+
IF ( 0 = (SELECT COUNT(*) FROM Sales.OrderLines) )
8+
RAISERROR('OrderLines must be loaded into the table before executing this script', 12, 5);
9+
10+
-- Insert single order line record in package type = 0
11+
-- This is prerequisite for Automatic tuning demo
12+
INSERT INTO [Sales].[OrderLines] (OrderID, StockItemID, Description, PackageTypeID, Quantity, UnitPrice, TaxRate, PickedQuantity, LastEditedBy)
13+
SELECT TOP 1 OrderID, StockItemID, 'Unique OrderLine for Unknown package type', 0, Quantity, UnitPrice, TaxRate, PickedQuantity, LastEditedBy
14+
FROM Sales.OrderLines
15+
WHERE Quantity IS NOT NULL AND UnitPrice IS NOT NULL
16+
ORDER BY NEWID();
17+
18+
COMMIT;

samples/databases/wide-world-importers/wwi-ssdt/wwi-ssdt/Sales/Tables/OrderLines.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ CREATE NONCLUSTERED INDEX [IX_Sales_OrderLines_Perf_20160301_02]
4949

5050
GO
5151
CREATE COLUMNSTORE INDEX [NCCX_Sales_OrderLines]
52-
ON [Sales].[OrderLines]([OrderID], [StockItemID], [Description], [Quantity], [UnitPrice], [PickedQuantity]);
52+
ON [Sales].[OrderLines]([OrderID], [StockItemID], [Description], [Quantity], [UnitPrice], [PickedQuantity], [PackageTypeID]);
5353

5454

5555
GO

samples/databases/wide-world-importers/wwi-ssdt/wwi-ssdt/WideWorldImporters.sqlproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,5 +748,6 @@
748748
<None Include="PostDeploymentScripts\pds150-ins-app-cities.sql" />
749749
<None Include="PostDeploymentScripts\pds105-ins-dls-ficticiousnamepool.sql" />
750750
<None Include="PostDeploymentScripts\pds106-ins-dls-areacode.sql" />
751+
<None Include="PostDeploymentScripts\pds400-ins-unkown-orderline.sql" />
751752
</ItemGroup>
752753
</Project>

samples/features/automatic-tuning/force-last-good-plan/sql-scripts/demo-full.sql

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
/********************************************************
22
* SETUP - clear everything
33
********************************************************/
4+
ALTER DATABASE SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE;
5+
ALTER DATABASE current SET QUERY_STORE CLEAR ALL;
46
ALTER DATABASE current SET AUTOMATIC_TUNING (FORCE_LAST_GOOD_PLAN = OFF);
5-
EXEC dbo.initialize;
67

78
/********************************************************
89
* PART I
@@ -20,21 +21,31 @@ GO 60
2021

2122
-- 2. Execute the procedure that causes plan regression
2223
-- Optionally, include "Actual execution plan" in SSMS and show the plan - it should have Stream Aggregate, Index Seek & Nested Loops
23-
EXEC dbo.regression;
24-
24+
ALTER DATABASE SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE;
25+
EXEC sp_executesql N'select avg([UnitPrice]*[Quantity])
26+
from Sales.OrderLines
27+
where PackageTypeID = @packagetypeid', N'@packagetypeid int',
28+
@packagetypeid = 0;
2529

2630
-- 3. Start the workload again - verify that is slower.
2731
EXEC sp_executesql N'select avg([UnitPrice]*[Quantity])
2832
from Sales.OrderLines
29-
where PackageTypeID = @packagetypeid', N'@packagetypeid int', @packagetypeid = 7;
33+
where PackageTypeID = @packagetypeid', N'@packagetypeid int',
34+
@packagetypeid = 7;
3035
go 20
3136
-- Optionally, include "Actual execution plan" in SSMS and show the plan - it should have Stream Aggregate with Non-clustered index seek.
3237

38+
39+
40+
3341
-- 4. Find a recommendation that can fix this issue:
3442
SELECT reason, score,
3543
script = JSON_VALUE(details, '$.implementationDetails.script')
3644
FROM sys.dm_db_tuning_recommendations;
3745

46+
47+
48+
3849
-- 4.1. Optionally get more detailed information about the regression and recommendation.
3950
SELECT reason, score,
4051
script = JSON_VALUE(details, '$.implementationDetails.script'),
@@ -81,7 +92,8 @@ GO 20
8192
/********************************************************
8293
* RESET - clear everything
8394
********************************************************/
84-
EXEC [dbo].[initialize];
95+
ALTER DATABASE SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE;
96+
ALTER DATABASE current SET QUERY_STORE CLEAR ALL;
8597

8698
-- Enable automatic tuning on the database:
8799
ALTER DATABASE current
@@ -95,17 +107,31 @@ WHERE name = 'FORCE_LAST_GOOD_PLAN';
95107
-- 1. Start workload - execute procedure 30-300 times like in the phase I
96108
EXEC sp_executesql N'select avg([UnitPrice]*[Quantity])
97109
from Sales.OrderLines
98-
where PackageTypeID = @packagetypeid', N'@packagetypeid int', @packagetypeid = 7;
99-
GO 60
110+
where PackageTypeID = @packagetypeid', N'@packagetypeid int',
111+
@packagetypeid = 7;
112+
GO 60 -- NOTE: This number shoudl be incrased if you don't get a plan change regression.
100113

101-
-- 2. Execute the procedure that causes the plan regression
102-
exec dbo.regression;
103114

104-
-- 3. Start the workload again - verify that it is slower.
115+
116+
-- 2. Cause the plan regression
117+
ALTER DATABASE SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE;
118+
EXEC sp_executesql N'select avg([UnitPrice]*[Quantity])
119+
from Sales.OrderLines
120+
where PackageTypeID = @packagetypeid', N'@packagetypeid int',
121+
@packagetypeid = 0;
122+
123+
124+
125+
126+
-- 3. Start the workload again.
105127
EXEC sp_executesql N'select avg([UnitPrice]*[Quantity])
106128
from Sales.OrderLines
107129
where PackageTypeID = @packagetypeid', N'@packagetypeid int', @packagetypeid = 7;
108-
go 30
130+
go 20
131+
132+
133+
134+
109135

110136
-- 4. Find a recommendation and check is it in "Verifying" or "Success" state:
111137
SELECT reason, score,

samples/features/automatic-tuning/force-last-good-plan/wwwroot/index.html

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,6 @@
3737
<div class="col-md-4">
3838
<h1>Automatic tuning</h1>
3939
</div>
40-
<div class="col-md-3 hidden">
41-
<div class="btn-group pull-right align-bottom" data-toggle="buttons">
42-
<label class="btn btn-default">
43-
<input type="radio" name="options" id="off" autocomplete="off"> OFF
44-
</label>
45-
<label class="btn btn-default active">
46-
<input type="radio" name="options" id="on" autocomplete="off" checked> ON
47-
</label>
48-
</div>
49-
</div>
5040
</div>
5141
<div class="row">
5242
<div class="col-md-12">
@@ -68,6 +58,16 @@ <h3>Enable FORCE LAST GOOD PLAN:</h3>
6858
ALTER DATABASE current
6959
SET AUTOMATIC_TUNING ( FORCE_LAST_GOOD_PLAN = ON);</pre>
7060
</div>
61+
<div class="col-md-6">
62+
<div class="btn-group pull-right align-bottom" data-toggle="buttons">
63+
<label class="btn btn-default active">
64+
<input type="radio" name="options" id="off" autocomplete="off" checked> OFF
65+
</label>
66+
<label class="btn btn-default">
67+
<input type="radio" name="options" id="on" autocomplete="off"> ON
68+
</label>
69+
</div>
70+
</div>
7171
</div>
7272
<script src="media/d3.v3.min.js"></script>
7373
<script src="media/viz.v1.0.0.min.js"></script>
@@ -76,17 +76,21 @@ <h3>Enable FORCE LAST GOOD PLAN:</h3>
7676
<script src="media/GraphVizGauge.js"></script>
7777
<script src="/api/demo/init"></script>
7878
<script>
79-
var gauge = new GraphVizGauge("svg", { to: 250 });
79+
var gauge = new GraphVizGauge("svg", { to: 100 });
8080
var perfData = [];
8181
setInterval(function () {
8282
$.ajax({
8383
url: "/api/demo",
8484
success: function (result) {
8585
(perfData.length == 10) && perfData.shift();
86-
perfData.push(result.y);
86+
if(result.y>0)
87+
perfData.push(result.y);
88+
console.log(perfData);
8789
var total = 0;
88-
for (i=0; i<perfData.length; i++) { total += perfData[i]; }
89-
var perf = Math.round(10000 / (total / 10.0)) / 10.0;
90+
for (i = 0; i < perfData.length; i++) { total += perfData[i]; }
91+
var avgDurationMS = total / perfData.length;
92+
var perf = Math.round(1000 / (avgDurationMS));
93+
console.log(perf);
9094
gauge.Data(perf);
9195
$("#speed").text(perf);
9296
}

0 commit comments

Comments
 (0)