The document provides an overview of SQL Server Query Store, including what it can do, how to set it up, how it works, available reports, usage scenarios, features in 2017 and later versions, troubleshooting, and best practices. Query Store collects query execution plans and runtime statistics to help identify regressions, top resource consumers, and wait stats. It can automatically correct plans and supports forcing historical plans. Reports provide insights on regressed queries, resource usage, and wait stats. Query Store is enabled per database and supports various configuration options.
5. What Query Store Can Do For Us?
• Regressed Queries
• Identify Top Consuming Queries
• Audit the history of query plans
• Identify Resource Consumption Patterns
6. What Query Store Can Do For Us?
• In 2017
• Wait Stats
• Automatic plan correction
• Show more statistics
• In 2019
• Show even more statistics
11. Query Store Options 2019
• Query Store Capture Mode = CUSTOM
Stale Capture Policy Threshold
Execution Count
Total Compile CPU Time MS
Total Execution CPU Time MS
13. Query Store Setup
• Per Database
• Nice to have Central Repository
• https://feedback.azure.com/forums/9
08035-sql-
server/suggestions/33815995-create-
central-repository-for-query-store
14. Query Store Trace Flags
• 7745 - Forces Query Store to not flush
data to disk on database shutdown.
• 7752 - Enables asynchronous loading of
database
15. GUI
• Right click the
database, the
click on
properties,
then Query
Store
17. Query Store Setup Summary
• Set your options especially size
• PRIMARY filegroup
• Per database
• Use CUSTOM if you have ad-hoc quereies
and are on 2019
19. How Query Store Collects
Information
• Query processed by Algebrizer and
sends it to the Query Optimizer
• Checks for forced plan or out pops
a new Execution Plan
• Query Plan is put into Plan Cache
• Async collects data into Query
Store Memory
• Async writes to disk
20. Where Query Store Collects Information
• Plan Store – query plan, query text, last execution time,
compile time
• Runtime Stats Store – all runtime stats for plans, cpu,
logical reads, logical writes, physical reads, memory,
tempdb, duration, execution count
• Wait Stats Store – wait categories per plan
22. Ways to Stabilize Perfomance
• Change code and/or schema
• Add RECOMPILE
• Manually get the best plan in the cache
• Use a plan guide
• Force a plan using query store
23. Regressed Queries
• Identify queries that have degraded in
performance
• See if there is plan that can be forced
easily
• Force the previous plan from the history
28. Top Consuming Resources
• Look at forcing a plan because of regression
• Missing indexes
• Make sure that the statistics are up-to-date
• Make sure that indexes used by the query are
defragmented
• Consider rewriting expensive query
34. Forcing Query Plans Manually
• Using reports you can easily find queries
with multiple plans and force a plan
• They are not schema-bound
• Should monitor for failures with Extended
Events
• When manually forced the plan is used
even when it is no longer optimal
35. A/B Testing
• Rolling out new application version
• Adding new hardware to the server
• Creating missing indexes for expensive
queries
39. Wait Stats
• New DMV sys.query_store_wait_stats
• 23 categories
• Tracks by category of wait stats such as
CPU or MEMORY
40. Automatic Plan Correction
• Recommendations surfaced via
sys.dm_db_tuning_recommendations
• Can automatically switch to last known good
plan
• Data is in DMV even if you don’t have the
option turned on
• Data is lost on reboot of SQL Server
41. Automatic Plan Correction Setup
ALTER DATABASE CURRENT
SET AUTOMATIC_TUNING
(FORCE_LAST_GOOD_PLAN = ON);
42. Monitoring Automatic Plan Correction
• Create an Extended Events session that captures
automatic tuning events
• automatic_tuning_error
• automatic_tuning_plan_regression_detec
tion_check_completed
• automatic_tuning_plan_regression_verif
ication_check_completed
• automatic_tuning_recommendation_expire
d
43. Summary of 2017 Features
• Automatic Plan Correction
• Wait Stats
46. Perfmon Counters
• Query Store CPU usage
• Query Store logical reads
• Query Store logical writes
• Query Store physical reads
47.
48. Wait Stats
• Ton of these, they all start with qds
• Three can be safely ignored:
• qds_async_queue
• qds_cleanup_stale_queries_task_main_loop_sleep
• qds_shutdown_queue
• Use Paul Randal’s script is the best way to look at wait
stats
49. Extended Events
• Over 50 events added
• Most can only be used by special trace
flags for Microsoft troubleshooting
• We will look at script for the important
ones
51. Best Practices
• Manage the size
• Set you Statistics Interval
• Make sure it is continuing to collect data
• Avoid using non-parameterized queries
• Avoid using drop/create process
• Avoid renaming databases
52. Using the Query Store with In-Memory
OLTP
• Turn on runtime statistics collection with
sys.sp_xtp_control_query_exec_stats
• Need to reset on server restart or if you
clear Query Store
• Memory grants metrics are not kept
53. Things of Note
• Forced plans can fail
• Drop an index
• Change the name of an index
• Remove columns from an index
• Forced plans for do work on the secondaries
of your AGs
• Forced plans don’t age out of Query Store
54. Resources
• Monitoring Performance By Using the Query Store - https://docs.microsoft.com/en-
us/sql/relational-databases/performance/monitoring-performance-by-using-the-query-
store
• Best Practice with the Query Store - https://msdn.microsoft.com/en-
us/library/mt604821.aspx
• Wait Stats - https://docs.microsoft.com/en-us/sql/relational-databases/system-catalog-
views/sys-query-store-wait-stats-transact-sql
• Automatic plan correction in SQL Server 2017 -
https://blogs.msdn.microsoft.com/sqlserverstorageengine/2017/05/17/automatic-plan-
correction-in-sql-server-2017/
• T-SQL Tuesday #124 Summary - https://tracyboggiano.com/archive/2020/03/t-sql-
tuesday-124-query-store-summary/
• Erin Stellato’s Blog - https://www.sqlskills.com/blogs/erin/category/query-store/
• Grant Fritchey’s Blog - https://www.scarydba.com/?s=query+store