Overview
Query Store records query text, execution plans and runtime statistics per time interval, inside the user database. It answers “what changed?” after a regression, survives restarts (unlike the plan cache DMVs), and lets you force a previous plan.
Core concepts
- Catalog views:
sys.query_store_query_text,sys.query_store_query,sys.query_store_plan,sys.query_store_runtime_stats,sys.query_store_runtime_stats_interval, and wait statistics insys.query_store_wait_stats. - Capture mode:
ALL,AUTO(skip trivial or rare queries),NONE,CUSTOM. Statistics are aggregated per interval (default 60 minutes) and flushed asynchronously;sp_query_store_flush_dbforces a flush. - Plan forcing:
EXEC sp_query_store_force_plan @query_id, @plan_idpins a plan; unforce when the data changes. Failures show inlast_force_failure_reason. - Automatic tuning (
FORCE_LAST_GOOD_PLAN) detects regressions and forces the last good plan; it needs Query Store and, on-premises, an edition that supports it (Enterprise). - Query Store hints (SQL Server 2022): attach hints such as
RECOMPILEorMAXDOPto a query without changing application code. - In SQL Server 2022, Query Store is on by default for new databases. Databases restored or upgraded from older versions keep their old setting.
Worked example
A fresh database on the test instance (SQL Server 2022):
SELECT actual_state_desc, query_capture_mode_desc, interval_length_minutes, max_storage_size_mb
FROM sys.database_query_store_options;
-- READ_WRITE | AUTO | 60 | 1000After running the stored procedure from sqlserver-parameter-sniffing, the top consumers by logical reads:
EXEC sp_query_store_flush_db;
SELECT TOP 3 qt.query_sql_text, rs.count_executions,
CAST(rs.avg_duration/1000.0 AS decimal(10,2)) AS avg_ms, rs.avg_logical_io_reads
FROM sys.query_store_query q
JOIN sys.query_store_query_text qt ON qt.query_text_id = q.query_text_id
JOIN sys.query_store_plan p ON p.query_id = q.query_id
JOIN sys.query_store_runtime_stats rs ON rs.plan_id = p.plan_id
ORDER BY rs.avg_logical_io_reads DESC;returned the procedure body with count_executions = 4, average 9.2-10.7 ms and 9,088 average logical reads for the scan plans, and a separate row for the OPTION (RECOMPILE) variant. Each distinct query text (even a one-character change) gets its own query_id.
Pitfalls and production notes
- Size
MAX_STORAGE_SIZE_MBand keepSIZE_BASED_CLEANUP_MODE = AUTO; a full store goes read-only and silently stops capturing. - Ad hoc workloads with unparameterised literals flood Query Store; use
AUTOcapture and forced parameterisation where appropriate. - Forced plans can become wrong when data shifts, and they can fail after schema changes; monitor them.
- Take a baseline before an upgrade or compatibility-level change, then compare (sqlserver-upgrades-compat).
- Reports in SSMS: Regressed Queries, Top Resource Consuming Queries, Tracked Queries.
Practice questions
- After a deployment one query is 20x slower. Which Query Store view helps, and what do you check?
- Why can Query Store not capture a query that ran once, in
AUTOmode? - When is plan forcing a bad idea?
References
- Microsoft Learn, Monitor performance by using the Query Store; Query Store hints
Related
sqlserver-execution-plans · sqlserver-parameter-sniffing · sqlserver-upgrades-compat · sqlserver-wait-stats