Skip to content

Commit 67b8f76

Browse files
authored
Query Store Hints Demo
Query Store Hints Demo
1 parent 85f6bb8 commit 67b8f76

1 file changed

Lines changed: 131 additions & 0 deletions

File tree

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
-- ************************************************************************ --
2+
-- Query Store hints demo
3+
4+
-- Demo uses "PropertyMLS" database which can be imported from BACPAC here:
5+
-- https://github.com/microsoft/sql-server-samples/tree/master/samples/features/query-store
6+
7+
-- Email QSHintsFeedback@microsoft.com for questions\feedback
8+
-- ************************************************************************ --
9+
10+
/*
11+
Demo prep, in PropertyMLS
12+
*/
13+
14+
ALTER DATABASE [PropertyMLS] SET QUERY_STORE CLEAR;
15+
ALTER DATABASE current SET QUERY_STORE = ON;
16+
ALTER DATABASE current SET QUERY_STORE (QUERY_CAPTURE_MODE = ALL);
17+
GO
18+
19+
-- Should be READ_WRITE
20+
SELECT actual_state_desc
21+
FROM sys.database_query_store_options;
22+
GO
23+
24+
/*
25+
You can verify Query Store Hints in sys.query_store_query_hints.
26+
Checking if any already exist (should be none).
27+
*/
28+
SELECT query_hint_id,
29+
query_id,
30+
query_hint_text,
31+
last_query_hint_failure_reason,
32+
last_query_hint_failure_reason_desc,
33+
query_hint_failure_count,
34+
source,
35+
source_desc
36+
FROM sys.query_store_query_hints;
37+
GO
38+
39+
/*
40+
The PropertySearchByAgent stored procedure has a parameter
41+
used to filter AgentId. Looking at the statistics for AgentId,
42+
you will see that there is a big skew for AgentId 101.
43+
*/
44+
SELECT hist.range_high_key AS [AgentId],
45+
hist.equal_rows
46+
FROM sys.stats AS s
47+
CROSS APPLY sys.dm_db_stats_histogram(s.[object_id], s.stats_id) AS hist
48+
WHERE s.[name] = N'NCI_Property_AgentId';
49+
50+
51+
-- Show actual query execution plan to see plan compiled.
52+
-- Agent with many properties will have a scan with parallelism.
53+
EXEC [dbo].[PropertySearchByAgent] 101;
54+
55+
-- Agents with few properties still re-use this plan (assuming no recent plan eviction).
56+
EXEC [dbo].[PropertySearchByAgent] 4;
57+
58+
59+
/*
60+
Now let's find the query_id associated with this query.
61+
*/
62+
SELECT query_sql_text, q.query_id
63+
FROM sys.query_store_query_text qt
64+
INNER JOIN sys.query_store_query q ON
65+
qt.query_text_id = q.query_text_id
66+
WHERE query_sql_text like N'%ORDER BY ListingPrice DESC%' and query_sql_text not like N'%query_store%';
67+
GO
68+
69+
/*
70+
We can set the hint associated with the query_id
71+
Note, we can designate one or more query hints
72+
*/
73+
EXEC sp_query_store_set_hints 10, N'OPTION(RECOMPILE)';
74+
GO
75+
76+
/*
77+
You can verify Query Store Hints in sys.query_store_query_hints
78+
*/
79+
SELECT query_hint_id,
80+
query_id,
81+
query_hint_text,
82+
last_query_hint_failure_reason,
83+
last_query_hint_failure_reason_desc,
84+
query_hint_failure_count,
85+
source,
86+
source_desc
87+
FROM sys.query_store_query_hints;
88+
GO
89+
90+
-- Execute both at the same time and show actual query execution plan.
91+
-- You should see two different plans, one for AgentId 101 and one for AgentId 4.
92+
EXEC [dbo].[PropertySearchByAgent] 101;
93+
EXEC [dbo].[PropertySearchByAgent] 4;
94+
GO
95+
96+
SELECT query_hint_id,
97+
query_id,
98+
query_hint_text,
99+
last_query_hint_failure_reason,
100+
last_query_hint_failure_reason_desc,
101+
query_hint_failure_count,
102+
source,
103+
source_desc
104+
FROM sys.query_store_query_hints;
105+
GO
106+
107+
/*
108+
We can remove the hint using sp_query_store_clear_query_hints
109+
*/
110+
EXEC sp_query_store_clear_hints @query_id = 10;
111+
GO
112+
113+
/*
114+
That Query Store Hint is now removed
115+
*/
116+
SELECT query_hint_id,
117+
query_id,
118+
query_hint_text,
119+
last_query_hint_failure_reason,
120+
last_query_hint_failure_reason_desc,
121+
query_hint_failure_count,
122+
source,
123+
source_desc
124+
FROM sys.query_store_query_hints;
125+
GO
126+
127+
-- Execute both at the same time and show actual query execution plan.
128+
-- You should see one plan again.
129+
EXEC [dbo].[PropertySearchByAgent] 101;
130+
EXEC [dbo].[PropertySearchByAgent] 4;
131+
GO

0 commit comments

Comments
 (0)