Force a Stable Execution Plan in SQL Server with Query Store
Stabilize a regressed SQL Server query by identifying the last good plan and forcing it with Query Store, then verify and unforce when conditions change.
08 Jul 2026, 23:15 UTC

The problem: a good query suddenly slows down
A query that previously returned in seconds can start taking minutes when the query optimizer chooses a different execution plan (the optimizer's strategy for running the query). Parameter sniffing, outdated statistics, or index changes can trigger a plan regression. The useful takeaway is that SQL Server Query Store can capture the last good plan and force the optimizer to reuse it, giving you a stable baseline while you investigate the root cause.
Desired outcome
You identify a regressed query in a user database, select a previously good execution plan from Query Store history, force that plan, and confirm that the optimizer is using it. You also know how to unforce the plan when data distribution or schema changes make it stale.
Prerequisites
- SQL Server 2016 or later, or Azure SQL Database. Query Store is a per-database feature.
- You are connected to the user database where the query runs, not the master database.
- You have ALTER DATABASE permission on that database.
- The database is writable. Forcing plans on read-only databases or Always On readable secondaries is not supported in the same way as on the primary writable database.
Enable Query Store on the database
Run the following in the target user database. Replace [YourDatabase] with the actual database name.
ALTER DATABASE [YourDatabase]
SET QUERY_STORE = ON
(OPERATION_MODE = READ_WRITE,
CLEANUP_POLICY = (STALE_QUERY_THRESHOLD_DAYS = 30),
DATA_FLUSH_INTERVAL_MINUTES = 900,
MAX_STORAGE_SIZE_MB = 1000);
Where to run: In the user database context. Permissions: ALTER DATABASE. Risk: Query Store stores plan and runtime data in the primary filegroup. If storage reaches MAX_STORAGE_SIZE_MB and auto-cleanup cannot keep up, Query Store can switch to read-only mode and stop capturing new data.
Find the regressed query and candidate plan
Query Store exposes queries, plans, and runtime statistics through system catalog views (database tables that expose metadata). Use sys.query_store_query, sys.query_store_plan, and sys.query_store_runtime_stats to locate the query and compare plans.
SELECT q.query_id,
p.plan_id,
p.is_forced,
rs.avg_duration,
rs.avg_cpu_time,
rs.avg_logical_reads
FROM sys.query_store_query AS q
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
WHERE q.object_id = OBJECT_ID(N'dbo.YourProcedureOrTable')
ORDER BY rs.avg_duration DESC;
Diagnostic decision: Look for a plan_id with significantly higher average duration, CPU, or logical reads than an earlier plan for the same query_id. The earlier, lower-cost plan is your candidate to force. If you do not know the object ID, query sys.query_store_query for recent query text and identify query_id from there.
Force the plan
Once you have the query_id and the plan_id you want to reuse, run:
EXEC sys.sp_query_store_force_plan @query_id = 123, @plan_id = 456;
Replace 123 and 456 with the actual values from your database. Permissions: ALTER DATABASE. Risk: A forced plan remains in effect even if data distribution changes. If an index supporting the plan is dropped, the plan can be invalidated and the optimizer will recompile, potentially choosing a worse plan. Review forced plans periodically rather than leaving them unattended.
Verify the force took effect
Check that Query Store is actually capturing data:
SELECT actual_state_desc, desired_state_desc
FROM sys.database_query_store_options;
Both values should be READ_WRITE. Then confirm the plan is marked as forced:
SELECT plan_id, is_forced
FROM sys.query_store_plan
WHERE query_id = 123;
is_forced should return 1 for the plan you forced. To check runtime impact, compare sys.query_store_runtime_stats before and after forcing for the same query_id, looking at duration, CPU, and logical reads.
Recovery: unforce or disable Query Store
Because forcing a plan changes database state, you should know how to reverse it.
Unforce a single plan
EXEC sys.sp_query_store_unforce_plan @query_id = 123, @plan_id = 456;
Afterward, is_forced returns to 0 and the optimizer is free to choose plans again.
Pause, clear, or turn off Query Store
ALTER DATABASE [YourDatabase] SET QUERY_STORE = OFF;
Use SET QUERY_STORE CLEAR to remove stored data while keeping the feature enabled, or OFF to stop collection entirely. Clearing or disabling removes forced plans and historical runtime statistics.
Limitations and ongoing checks
- Query Store consumes disk space in the primary filegroup. Monitor
sys.database_query_store_optionsand adjustMAX_STORAGE_SIZE_MBand cleanup policy as needed. - Schema changes such as dropped indexes can invalidate forced plans. The optimizer will recompile and may ignore the forced plan.
- Plan forcing is not a substitute for fixing missing indexes or outdated statistics. Treat it as a stabilization step while you address the underlying cause.
- This procedure applies to user databases on a writable primary. Read-only databases and Always On secondaries do not support plan forcing in the same way.
Practical check: After forcing, run the target query during a representative workload and confirm sys.query_store_plan.is_forced = 1 and stable runtime statistics. If performance regresses again, unforce the plan and reassess indexes and statistics.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.