Added Query Store demo

This commit is contained in:
pmasl
2019-04-20 20:57:53 -07:00
parent 7cc629150c
commit 68a5d6f9f9
8 changed files with 272 additions and 0 deletions
@@ -0,0 +1,61 @@
USE [AdventureWorks2016_EXT]
GO
/* (1) Do cardinality analysis when suspect on ad-hoc workloads*/
SELECT COUNT(*) AS CountQueryTextRows FROM sys.query_store_query_text;
SELECT COUNT(*) AS CountQueryRows FROM sys.query_store_query;
SELECT COUNT(DISTINCT query_hash) AS CountDifferentQueryRows FROM sys.query_store_query;
SELECT COUNT(*) AS CountPlanRows FROM sys.query_store_plan;
SELECT COUNT(DISTINCT query_plan_hash) AS CountDifferentPlanRows FROM sys.query_store_plan;
/* (2) Get Compile Vs Execution times: ad-hoc workloads tend to spend lot of time in compilation*/
EXEC sp_GetCompilAndExecutionTotalTime
/* (3) See query pattern*/
SELECT TOP 10 * FROM sys.query_store_query_text
/* (4) I'm not getting new queries?
Look at Query Store parameters - is Query Store in READ_ONLY mode?
*/
SELECT current_storage_size_mb, max_storage_size_mb, desired_state, desired_state_desc, actual_state, actual_state_desc, readonly_reason, flush_interval_seconds,
interval_length_minutes, stale_query_threshold_days, max_plans_per_query, query_capture_mode, query_capture_mode_desc, size_based_cleanup_mode,
size_based_cleanup_mode_desc, actual_state_additional_info
FROM sys.database_query_store_options
ALTER DATABASE [AdventureWorks2016_EXT] SET QUERY_STORE CLEAR;
ALTER DATABASE [AdventureWorks2016_EXT] SET QUERY_STORE = ON (OPERATION_MODE = READ_WRITE);
GO
/* (5) How do we fix the Auto-Param problem?*/
/* At the query level: apply the plan guide for selected query template */
DECLARE @stmt nvarchar(max);
DECLARE @params nvarchar(max);
EXEC sp_get_query_template
N'select * from part p join partdetails pp on p.partid = pp.partid where p.partid = 46911',
@stmt OUTPUT,
@params OUTPUT;
EXEC sp_create_plan_guide
N'TemplateGuide1',
@stmt,
N'TEMPLATE',
NULL,
@params,
N'OPTION(PARAMETERIZATION FORCED)';
/*(6) Alternative (at the database level): force parametrization for all queries*/
ALTER DATABASE [AdventureWorks2016_EXT] SET PARAMETERIZATION FORCED;
/* Run analysis query (1), (2) again to see results of parametrization */
/*(7) Reset the DB state*/
ALTER DATABASE [AdventureWorks2016_EXT] SET PARAMETERIZATION SIMPLE;
GO
EXEC sp_control_plan_guide N'DROP', N'TemplateGuide1';
GO
ALTER DATABASE [AdventureWorks2016_EXT] SET QUERY_STORE CLEAR;
ALTER DATABASE [AdventureWorks2016_EXT] SET QUERY_STORE = ON (OPERATION_MODE = READ_WRITE);
SELECT * FROM sys.database_query_store_options
GO
Binary file not shown.
Binary file not shown.

After

Width:  |  Height:  |  Size: 390 KiB

+50
View File
@@ -0,0 +1,50 @@
**Query Store demo**
This demo shows capabilities of Query Store. Usually we demo 3-4 scenarios:
1. How to turn on and initially configure Query Store
2. How Query Store collects & exposed data
3. Detecting and fixing query with plan choice regression
4. Detecting and fixing workload that is candidate for auto-parametrization
**Prerequisites**
Restore AdventureWorks2016_EXT database from the provided BAK at https://github.com/Microsoft/sql-server-samples/releases/tag/adventureworks.
Turn ON and Configure Query Store
After restoring the database, go to Properties / Query Store tab.
![Query Store in SSMS](../QS_SSMS.png)
Use docs content to walk through main config settings: https://docs.microsoft.com//sql/relational-databases/performance/best-practice-with-the-query-store#Configure
**How Query Store Works**
Open ShowBasics.sql script and execute queries individually:
- Run simple `SELECT * FROM` Part
- Show where query ends in sys.query_store_query_text, sys.query_store_query, sys.query_store_plan, sys.query_store_runtime_stats
- Use custom view `vw_QueryStoreCompileInfo` to get info more easily. The main point here is: people can write their own scripts combining Query Store views
- Execute the same user query from the proc, using sp_executesql, trigger and show that containing object defines query identity in QDS (each instance of the same query text becomes separate query that can be monitored and tuned independently)
- Show what happens with query that gets auto-parametrized. It cannot be searched using the original query text because QDS stores query as parametrized. Hopefully, sys.fn_stmt_sql_handle_from_sql_stmt can be used to track down query using original query text
- Run `vw_QueryStoreRuntimeInfo` (again custom view) to show main runtime stats combined with query/plan info
**Query with plan regression**
1. Run QueryStoreSimpleDemo.exe with option R or option S
2. Open SSMS, analyze and explain - two execution plans that SQL Server use alternately (switches between 2 plan almost randomly). This is known as Parameter Sniffing problem - plan gets generated based on parameter available at the compilation time. When compilation happens frequently and randomly and data is skewed (not all parameter values are uniformly distributed PSP is likely to occur and degradations are common)
3. Force better plan, explain what happens (SSMS)
4. Summarize benefits for DBA fixing performance quickly without knowing details about the query. Fully transparent to running apps
Detect and fix ad hoc workload that is candidate for parametrization
1. Run QueryStoreSimpleDemo.exe with option P and let it work for some time (15-20 sec)
2. Open “Auto-Param Analysis.sql” and run queries from groups (1) and (2). What we see is:
a. Large number of queries / plan entries, small number of different query/plan hashes indicates queries that are not parametrized although they are good candidates
b. Relatively big compile time shows that system wastes resources on compilation instead of execution
3. Open SSMS Top Resource Consuming queries if you increase number of presented queries to 50 youll see that majority of queries has similar /negligible consumption theres nothing user can optimize/tune. This is what we call “death by a thousand of cuts”
4. Run (3) query to see query text pattern it becomes obvious that queries differ only by provided literal value
5. If you run (1) youll notice that numbers do not change although workload is running. (4) gives us the answer Query Store went to READ_ONLY due to large number of queries / plans. This is another point you should make: ad-hoc queries are not bad for SQL Server & execution but also for Query Store as it goes to RO mode which means we do not operate with latest facts!
6. Run (5) to parametrize query and clear Query Store. Workload is still running!
7. Run (1) again to see numbers now: ration between count(queries) and count(distinct query_hash) is now near to 1.
8. Open Open SSMS Top Resource Consuming queries: youll see dozen of different queries to tune
9. (6) show alternative solution applying forced parametrization for the entire DB. Just mention, as a possible solution
10. Run (7) to reset DB to initial state.
@@ -0,0 +1,96 @@
/*Clear Query Store and procedure cache*/
ALTER DATABASE AdventureWorks2016_EXT SET QUERY_STORE CLEAR;
ALTER DATABASE AdventureWorks2016_EXT SET QUERY_STORE = ON (QUERY_CAPTURE_MODE = ALL);
DBCC FREEPROCCACHE
GO
USE AdventureWorks2016_EXT;
GO
/*Run simple query - what data is collected and where does it go to?*/
SELECT * FROM Part;
SELECT * FROM sys.query_store_query_text;
SELECT * FROM sys.query_store_query;
SELECT * FROM sys.query_store_plan;
SELECT * FROM sys.query_store_runtime_stats;
/*
Combine all info
vw_QueryStoreCompileInfo is custom view (created for presentation)
*/
SELECT * FROM vw_QueryStoreCompileInfo
WHERE query_sql_text = 'SELECT * FROM Part'
/*The same query from the proc*/
DROP PROCEDURE IF EXISTS sp_GetParts
GO
CREATE PROCEDURE sp_GetParts
AS
SELECT * FROM Part;
GO
EXEC sp_GetParts;
/*Again the same query, from sp_executesql*/
EXEC sp_executesql N'SELECT * FROM Part'
SELECT * FROM vw_QueryStoreCompileInfo
WHERE query_sql_text = 'SELECT * FROM Part'
/*Finally trigger*/
DROP TRIGGER IF EXISTS dbo.OnPartInsert
GO
CREATE TRIGGER dbo.OnPartInsert
ON dbo.Part
AFTER INSERT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SELECT * FROM Part;
END
GO
INSERT INTO Part VALUES (3000020, 'Part_300020');
SELECT * FROM vw_QueryStoreCompileInfo
WHERE query_sql_text = 'SELECT * FROM Part'
/*What happens with parametrized query?*/
SELECT * FROM Part WHERE PartId = 5;
SELECT * FROM vw_QueryStoreCompileInfo
WHERE query_sql_text = 'SELECT * FROM Part = 5'
/* Check sys.query_store_query_text */
SELECT * FROM sys.query_store_query_text;
/*Try sys.fn_stmt_sql_handle_from_sql_stmt this instead*/
SELECT * FROM sys.fn_stmt_sql_handle_from_sql_stmt
('SELECT * FROM Part WHERE PartId = 5', NULL)
/*Changed searched criteria*/
SELECT V.* FROM vw_QueryStoreCompileInfo V
JOIN sys.fn_stmt_sql_handle_from_sql_stmt
('SELECT * FROM Part WHERE PartId = 5', NULL) F
ON V.statement_sql_handle = F.statement_sql_handle
/*Get runtime info for the queries*/
SELECT * FROM vw_QueryStoreRuntimeInfo
WHERE query_sql_text = 'SELECT * FROM Part'
ORDER BY start_time DESC
SELECT * FROM vw_QueryStoreRuntimeInfo V
JOIN sys.fn_stmt_sql_handle_from_sql_stmt
('SELECT * FROM Part WHERE PartId = 5', NULL) F
ON V.statement_sql_handle = F.statement_sql_handle
ORDER BY start_time DESC
@@ -0,0 +1,25 @@
USE [AdventureWorks2016_EXT]
GO
DROP PROCEDURE IF EXISTS sp_GetCompilAndExecutionTotalTime
GO
CREATE PROCEDURE sp_GetCompilAndExecutionTotalTime
AS
DECLARE @totalCompiles int
DECLARE @totalExecutions int
DECLARE @totalCompileTime decimal(18,4)
DECLARE @totalExecutionTime decimal(18,4)
SELECT @totalCompiles = SUM(count_compiles),
@totalCompileTime = SUM(count_compiles * avg_compile_duration / 1000.)
FROM sys.query_store_plan;
SELECT @totalExecutions = SUM(count_executions),
@totalExecutionTime = SUM(count_executions * avg_duration / 1000.)
FROM sys.query_store_runtime_stats
SELECT @totalCompiles AS TotalCompiles, @totalExecutions AS TotalExecutions,
@totalCompileTime AS TotalCompileTime, @totalExecutionTime AS TotalDurationTime
GO
@@ -0,0 +1,19 @@
USE [AdventureWorks2016_EXT]
GO
DROP VIEW IF EXISTS [vw_QueryStoreCompileInfo];
GO
CREATE VIEW [dbo].[vw_QueryStoreCompileInfo]
AS
SELECT qt.query_text_id, q.query_id, p.plan_id, qt.query_sql_text, s.name AS ContainingSchema, o.name AS ContainingObject, q.query_hash, qt.statement_sql_handle, q.is_internal_query,
q.query_parameterization_type_desc, q.count_compiles AS query_count_compiles, p.query_plan_hash, p.count_compiles AS plan_count_compiles, p.last_compile_start_time, p.engine_version,
p.compatibility_level, p.query_plan, p.is_trivial_plan, p.is_parallel_plan, p.is_forced_plan
FROM sys.query_store_query_text AS qt INNER JOIN
sys.query_store_query AS q ON qt.query_text_id = q.query_text_id INNER JOIN
sys.query_store_plan AS p ON q.query_id = p.query_id LEFT OUTER JOIN
sys.objects AS o ON q.object_id = o.object_id LEFT OUTER JOIN
sys.schemas AS s ON s.schema_id = o.schema_id
GO
@@ -0,0 +1,21 @@
USE [AdventureWorks2016_EXT]
GO
DROP VIEW IF EXISTS [dbo].[vw_QueryStoreRuntimeInfo]
GO
CREATE VIEW [dbo].[vw_QueryStoreRuntimeInfo]
AS
SELECT qt.query_text_id, q.query_id, p.plan_id, qt.query_sql_text, s.name AS ContainingSchema, o.name AS ContainingObject, qt.statement_sql_handle, rsi.start_time, rsi.end_time, rs.execution_type_desc,
rs.count_executions, rs.avg_duration, rs.max_duration, rs.avg_cpu_time, rs.max_cpu_time, rs.avg_logical_io_reads, rs.max_logical_io_reads, rs.avg_physical_io_reads, rs.max_physical_io_reads,
rs.avg_logical_io_writes, rs.max_logical_io_writes, rs.avg_query_max_used_memory, rs.max_query_max_used_memory, rs.avg_rowcount, rs.max_rowcount, rs.avg_dop, rs.max_dop
FROM sys.query_store_query_text AS qt INNER JOIN
sys.query_store_query AS q ON qt.query_text_id = q.query_text_id INNER JOIN
sys.query_store_plan AS p ON q.query_id = p.query_id LEFT OUTER JOIN
sys.objects AS o ON q.object_id = o.object_id LEFT OUTER JOIN
sys.schemas AS s ON s.schema_id = o.schema_id INNER JOIN
sys.query_store_runtime_stats AS rs ON rs.plan_id = p.plan_id INNER JOIN
sys.query_store_runtime_stats_interval AS rsi ON rsi.runtime_stats_interval_id = rs.runtime_stats_interval_id
GO