Fix SQL Server Execution-Plan Regressions with Query Store
Detect SQL Server execution-plan regressions in Query Store and force a known-good plan to stabilize query performance.
14 Apr 2026, 22:42 UTC

Problem and takeaway
A query that previously completed quickly suddenly runs slowly. The cause is often an execution-plan regression: SQL Server compiled a new plan after statistics changed, a parameter value shifted, or an index was added or removed, and the new plan performs worse than the previous one. Query Store, available in SQL Server 2016 and later, Azure SQL Database, and Azure SQL Managed Instance, captures query text, runtime statistics, and execution plans automatically. You can compare plans for the same query, identify the regressed plan, and force a known-good plan_id for that query_id until you remove the force.
Prerequisites
- SQL Server 2016 or later, Azure SQL Database, or Azure SQL Managed Instance. Query Store is not available in earlier on-premises versions.
- ALTER permission on the target database, typically through db_owner or a server-level role with database alteration rights.
- Available disk space for the database; Query Store writes internal tables inside the user database.
- The database must be read-write; Query Store cannot capture data in a read-only database.
Defaults for capture mode, retention, and size limits differ by edition and version. SQL Server 2016, 2019, 2022, and Azure SQL Database do not all share the same defaults; confirm the settings for your target version before applying them.
Enable Query Store
Run in the user database, not in master. Replace YourDatabase with the actual database name.
USE YourDatabase;
GO
ALTER DATABASE CURRENT
SET QUERY_STORE = ON
(OPERATION_MODE = READ_WRITE,
CLEANUP_POLICY = (STALE_QUERY_THRESHOLD_DAYS = 30),
DATA_FLUSH_INTERVAL_SECONDS = 900,
MAX_STORAGE_SIZE_MB = 1000,
QUERY_CAPTURE_MODE = AUTO);
GO
Where to run: SQL Server Management Studio, Azure Data Studio, or sqlcmd connected to the target database. Required permissions: ALTER on the database. Risk: enabling Query Store consumes disk space and adds overhead to compilation and execution. If MAX_STORAGE_SIZE_MB is reached, Query Store can switch to READ_ONLY automatically and stop capturing new plans.
Expected check:
SELECT desired_state_desc,
actual_state_desc,
current_storage_size_mb,
max_storage_size_mb
FROM sys.database_query_store_options;
Both state columns should return READ_WRITE for active capture. If actual_state_desc returns READ_ONLY, increase the storage limit or clear old data before proceeding.
Find a regressed plan
Query Store stores one or more plans per query_id. To find a regression, compare average duration, CPU, or logical reads across plans for the same query.
SELECT q.query_id,
p.plan_id,
p.is_forced_plan,
rs.avg_duration,
rs.avg_cpu_time,
rs.avg_logical_reads,
rs.exec_count
FROM sys.query_store_query q
JOIN sys.query_store_plan p ON q.query_id = p.query_id
JOIN sys.query_store_runtime_stats rs ON p.plan_id = rs.plan_id
WHERE q.object_id = OBJECT_ID('dbo.YourProcedure')
ORDER BY q.query_id, rs.avg_duration DESC;
Placeholder: replace dbo.YourProcedure with the actual object, or remove the WHERE clause to review all queries. Look for a query_id with multiple plan_ids where one plan has significantly higher avg_duration than another. The higher-duration plan is the regression candidate; the lower-duration plan is the known-good candidate.
Diagnostic decision: do not force a plan based on a single execution. Compare multiple runtime intervals and confirm the candidate plan was stable before the regression.
Force the known-good plan
Once you identify the better plan_id, force it:
EXEC sys.sp_query_store_force_plan
@query_id = 123,
@plan_id = 456;
Placeholders: @query_id is the query identifier from sys.query_store_query; @plan_id is the specific plan you want to use. Required permissions: ALTER on the database or db_owner membership. Risk: plan forcing applies to one plan for one query_id. It does not generalize across different parameter values or ad-hoc batch hashes. If data distribution changes significantly, the forced plan can become worse than the optimizer's choice.
Expected check after forcing:
SELECT q.query_id,
p.plan_id,
p.is_forced_plan,
rs.avg_duration
FROM sys.query_store_query q
JOIN sys.query_store_plan p ON q.query_id = p.query_id
JOIN sys.query_store_runtime_stats rs ON p.plan_id = rs.plan_id
WHERE q.query_id = 123;
is_forced_plan should return 1 for the forced plan. Subsequent runtime stats should reflect that plan_id.
Limitations
- Query Store retains data until size or age quotas are reached. Default retention and capture settings vary by edition and version; Azure SQL Database defaults differ from on-premises SQL Server.
- Forcing a plan does not solve parameter-sniffing problems for all parameter values. Test multiple parameter sets before forcing.
- Schema changes, index changes, or major statistics updates can make a forced plan suboptimal. Review forced plans periodically.
- Query Store adds storage and CPU overhead. Monitor current_storage_size_mb against max_storage_size_mb.
Recovery options
Enabling Query Store and forcing a plan both change database state. To undo a forced plan:
EXEC sys.sp_query_store_unforce_plan
@query_id = 123,
@plan_id = 456;
To stop Query Store capture without removing the database:
ALTER DATABASE CURRENT SET QUERY_STORE = OFF;
Clearing all Query Store data is version-dependent; consult your version's documentation before using any clear or reset command, because syntax and availability differ between SQL Server 2016, later on-premises releases, and Azure SQL Database.
Practical verification
Run the target query or workload after forcing the plan. Check sys.query_store_runtime_stats for the forced plan_id and confirm duration or CPU returns to the expected range. Recheck after statistics maintenance, index rebuilds, or application deployments that change data distribution.