A production query can become slow even when no application code has visibly changed. Updated statistics, changing data distribution, a new index, a compatibility-level change, or a different optimizer decision can all alter the behavior of the same SQL statement.
In these incidents, looking only at the current execution plan is not enough. The plan cache usually shows what is being used now, but it may not show which plan was active before the incident or exactly when performance changed. SQL Server Query Store addresses this gap by retaining query text, plan history, and runtime statistics across time intervals.
What problem does Query Store solve?
Think of Query Store as a performance timeline for database queries. When the same query has multiple execution plans, you can compare when each plan was used and how much work it performed during that period.
For example, an order-report query may average 250 milliseconds on Monday and 8 seconds on Tuesday. The query text can remain unchanged while the selected plan changes. Query Store makes that transition visible. The investigation can move from “why is the application slow?” to “which query changed plans, when did it change, and how much did its runtime increase?”
Enable Query Store deliberately
Query Store is available in SQL Server 2016 and later. Default behavior depends on the SQL Server version and how the database was created, so production databases should still be checked explicitly for operation mode, retention, and storage limits.
ALTER DATABASE [SalesDb]
SET QUERY_STORE = ON
(
OPERATION_MODE = READ_WRITE,
WAIT_STATS_CAPTURE_MODE = ON,
INTERVAL_LENGTH_MINUTES = 15,
MAX_STORAGE_SIZE_MB = 1024,
CLEANUP_POLICY = (STALE_QUERY_THRESHOLD_DAYS = 30)
);These values are examples, not universal recommendations. Tune them according to workload volume, query diversity, and available disk capacity. Query Store should not grow without bounds. Once the configured storage limit is reached, cleanup and collection behavior should be monitored.
You can inspect data immediately after enabling the feature, but decisions made before representative workload data is collected can be misleading. Systems with strong daily or weekly traffic patterns should capture a baseline that includes their normal business cycle.
Find a real performance regression
Consider an e-commerce platform where an order report previously completed in about 300 milliseconds but began taking 9 seconds after a deployment. Application logs show only that the endpoint is slow. Query Store reveals two plans for the same query and shows that the slower plan became active in the newer time interval.
Start by comparing these measurements:
- Average and total duration
- CPU consumption
- Logical and physical reads
- Execution count and wait time
Do not rely only on estimated cost shown in an execution plan. Compare the plans using actual runtime statistics across equivalent workload intervals. A plan with a lower estimated cost can still scan more rows, spill to tempdb, or request a larger memory grant on real data.
Use Query Store reports for fast triage
In SQL Server Management Studio, open the Regressed Queries report under the database's Query Store node. Narrow the investigation by time range, comparison metric, and baseline type. When a query has multiple plans, compare the plan summary and graphical differences.
Look carefully for changes such as:
- An Index Seek changing to an Index Scan or Table Scan
- A join type becoming expensive for the current data volume
- A large difference between estimated and actual row counts
- A larger memory grant or a tempdb spill
- A significant increase in CPU, I/O, or lock waits
Validate findings with catalog views
The graphical reports are useful for an incident, but recurring operations benefit from queryable reports. The following example lists queries and plans observed during the last 48 hours together with basic runtime statistics.
SELECT
q.query_id,
p.plan_id,
rs.execution_type_desc,
rs.count_executions,
rs.avg_duration,
rs.avg_cpu_time,
rs.avg_logical_io_reads,
rsi.start_time,
qt.query_sql_text
FROM sys.query_store_query_text AS qt
JOIN sys.query_store_query AS q
ON q.query_text_id = qt.query_text_id
JOIN sys.query_store_plan AS p
ON p.query_id = q.query_id
JOIN sys.query_store_runtime_stats AS rs
ON rs.plan_id = p.plan_id
JOIN sys.query_store_runtime_stats_interval AS rsi
ON rsi.runtime_stats_interval_id = rs.runtime_stats_interval_id
WHERE rsi.start_time >= DATEADD(HOUR, -48, SYSUTCDATETIME())
ORDER BY rs.avg_duration DESC;Before comparing avg_duration or related fields with another monitoring system, verify their units and reporting format. Query Store aggregates runtime statistics into time intervals, so the result describes the plan's behavior during an interval rather than one individual execution.
When should you force a plan?
If the earlier plan is demonstrably better and the newer plan is proven to be the cause of the regression, you can temporarily force the known-good plan through Query Store.
EXEC sys.sp_query_store_force_plan
@query_id = 125,
@plan_id = 412;Plan forcing can restore performance quickly, but it is not a permanent fix. Data distribution may change, the query's requirements may evolve, or the forced plan may perform poorly for different parameter values. Treat plan forcing as an incident-response measure and create a follow-up task for root-cause remediation.
For the permanent fix, determine why the optimizer selected the new plan. The solution may involve updating statistics, correcting an index, moving filtering earlier in the query, reviewing parameter usage, or rewriting the query. If parameter values represent very different data distributions, parameter-sensitive plan behavior should also be investigated.
Read wait statistics alongside the plan
A slow query does not always indicate high CPU or a bad access path. Blocking, storage I/O, memory pressure, and parallelism-related waits can also degrade user experience. Query Store helps examine waits in the context of a specific query and plan.
For example, high LCK_M_X waits may point to long-running modifications against the same data. High PAGEIOLATCH_SH waits should lead you to inspect physical reads and index access. RESOURCE_SEMAPHORE can indicate memory-grant pressure, while SOS_SCHEDULER_YIELD can justify a CPU-focused investigation. None of these waits is a complete diagnosis by itself; interpret them together with the plan, query text, and concurrent workload.
A safe production workflow
- Enable Query Store and define storage and retention limits.
- Collect a baseline that covers representative traffic periods.
- Confirm the regression using duration, CPU, I/O, and wait measurements.
- Compare the old and new plans using real runtime statistics.
- Temporarily force the known-good plan when appropriate.
- Investigate statistics, indexes, query shape, and parameter behavior for the permanent fix.
- Remove the forced plan only after the corrected behavior has been measured.
The purpose of Query Store is not to force a plan for every query. Its real value is connecting performance changes to historical evidence and making remediation measurable. In a mature process, plan forcing is an emergency brake; root-cause analysis is the permanent repair.
Sources
- https://learn.microsoft.com/sql/relational-databases/performance/monitoring-performance-by-using-the-query-store
- https://learn.microsoft.com/en-us/sql/relational-databases/performance/query-store-usage-scenarios?view=sql-server-ver17%2F%2F
- https://learn.microsoft.com/en-us/sql/relational-databases/performance/tune-performance-with-the-query-store?view=sql-server-ver17
- https://learn.microsoft.com/en-us/sql/relational-databases/performance/best-practice-with-the-query-store?view=sql-server-ver17
0 Comments