A SQL query can be fast today and noticeably slower tomorrow even when the application code has not changed. The cause is not always a missing index or poorly written SQL. Changes in statistics, schema modifications, index creation, data distribution, or plan cache eviction can lead SQL Server to choose a different execution plan.
Investigating this kind of plan regression is difficult when historical evidence is unavailable. SQL Server Query Store makes the investigation practical by persisting query text, execution plans, and aggregated runtime statistics inside the database. Instead of simply reporting that “the query is slow,” you can identify when performance changed and which plan was active at that time.
What does Query Store collect?
Query Store organizes query performance information into three main areas:
- Query text: The submitted SQL text and its relationship to Query Store query identifiers.
- Plan store: Multiple execution plans that were used by the same query over time.
- Runtime and wait statistics: Aggregated duration, CPU, logical reads, writes, memory, and wait information for defined time intervals.
The plan cache is useful for understanding current activity, but it generally does not provide a durable history of every plan used in the past. Query Store fills that gap. This is especially useful after a release, index operation, or statistics change appears to coincide with a performance problem.
Check the Query Store state first
Before analyzing a regression, confirm that Query Store is still collecting data for the target database. The following query displays its operational state, storage usage, and read-only reason:
USE [SalesDb];
GO
SELECT
actual_state_desc,
desired_state_desc,
current_storage_size_mb,
max_storage_size_mb,
readonly_reason,
query_capture_mode_desc,
size_based_cleanup_mode_desc
FROM sys.database_query_store_options;In a normally collecting configuration, actual_state_desc should be READ_WRITE. If the Query Store reaches its storage limit or encounters another administrative condition, it can enter READ_ONLY mode. Existing reports may still be visible, but new performance evidence will not be collected.
A reasonable baseline configuration
There is no universal configuration for every workload. For a production database that runs continuously, automatic cleanup and a bounded retention period are important starting points:
ALTER DATABASE [SalesDb]
SET QUERY_STORE (
OPERATION_MODE = READ_WRITE,
QUERY_CAPTURE_MODE = AUTO,
SIZE_BASED_CLEANUP_MODE = AUTO,
CLEANUP_POLICY = (STALE_QUERY_THRESHOLD_DAYS = 30)
);The AUTO capture mode can reduce the amount of less useful query data that is stored. High-throughput systems with many unique queries should still monitor Query Store size, plan counts, and retention behavior. A configuration that is too small can remove the evidence needed for troubleshooting; a configuration that is too broad can consume unnecessary database space.
How to find a regression
SQL Server Management Studio provides Query Store reports that are useful for an initial investigation. Open the database, expand Query Store, and use the Regressed Queries report to find queries whose duration or resource usage increased during a selected period.
Work through the investigation with these questions:
- When did the slowdown begin?
- Did the query use a different plan before and after that time?
- How much did duration, CPU, or logical reads change?
- Did the change coincide with a deployment, index operation, statistics update, or significant data change?
- Does the slowdown affect all parameter values or only particular values?
The last question needs careful interpretation. Query Store can expose different plans for parameterized statements and can reveal inconsistent behavior across executions. However, that observation alone does not prove parameter sniffing is the root cause. Data distribution, statistics quality, plan shape, and runtime conditions must be considered together.
Query Query Store views for your own reports
SSMS reports are convenient, but the Query Store catalog views allow you to build repeatable operational reports. The following example lists query plans ordered by average duration:
SELECT TOP (50)
q.query_id,
p.plan_id,
rs.count_executions,
rs.avg_duration,
rs.avg_cpu_time,
rs.avg_logical_io_reads,
rs.first_execution_time,
rs.last_execution_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
ORDER BY rs.avg_duration DESC;Do not use this output as an automatic decision engine. Averages can hide differences between time windows, parameter values, and execution counts. For a meaningful comparison, examine the same reporting interval and consider how frequently each plan ran.
How to choose a good plan
The plan with the lowest average duration is not automatically the best plan for the workload. Evaluate the following dimensions together:
- Elapsed duration and CPU consumption.
- Logical and physical read activity.
- Execution count and the query’s total contribution to workload cost.
- Memory grants, spills, and unusual tempdb consumption.
- Consistency across representative parameter values.
- Compatibility with current table size, data distribution, indexes, and statistics.
A plan that was efficient several months ago may no longer be appropriate after data growth or distribution changes. Prefer the most recent known good plan that performed well under a comparable workload rather than blindly selecting the oldest or cheapest-looking plan.
Use plan forcing as a controlled mitigation
If application changes cannot be deployed immediately and a clear plan regression has been confirmed, you can force a plan already recorded by Query Store:
EXEC sys.sp_query_store_force_plan
@query_id = 123,
@plan_id = 456;Forcing instructs SQL Server to try to use the selected plan when it encounters the query. It is not an unconditional guarantee. If the plan cannot be applied, SQL Server can optimize the query normally, and an Extended Event is generated for the forcing failure.
Plan forcing is a mitigation, not proof that the underlying problem has been solved. Investigate stale statistics, query design, indexing, data distribution, and environmental waits separately. Monitor the result under real workload, then remove forcing when a durable fix is available:
EXEC sys.sp_query_store_unforce_plan
@query_id = 123,
@plan_id = 456;A safe operational workflow
- Record the time window in which the regression began and identify the affected query_id.
- Compare at least one earlier plan with the current plan.
- Review duration, CPU, reads, waits, and execution count over comparable intervals.
- Correlate the plan change with release, maintenance, index, and statistics history.
- Force the most recent known good plan only when the evidence supports it.
- Monitor production behavior across representative parameter values and traffic patterns.
- Implement the root-cause fix and reassess whether the forced plan should be removed.
Think of plan forcing as an emergency brake: it can stabilize service quickly, but it does not explain why the system started accelerating.
Common mistakes
- Forcing a plan for every slow query: The actual problem may be blocking, I/O, resource pressure, or an application issue.
- Ignoring read-only status: Historical data can remain visible while current collection has stopped.
- Looking only at duration: CPU, reads, waits, memory, and execution count provide essential context.
- Leaving a forced plan forever: Workload and data characteristics change over time.
- Testing with one parameter only: Different selectivity levels can produce very different plan behavior.
Conclusion
Query Store turns SQL Server performance troubleshooting from a point-in-time guess into a historical comparison. The most reliable process is to verify the plan change, compare resource metrics, apply the latest known good plan only as a controlled mitigation, and then address the underlying cause. Regular cleanup, an appropriate capture mode, and periodic review of forced plans keep the process useful over the long term.
0 Comments