Advertisement
Intermediate SQL Server, Query Store

How do you force SQL Server to use a specific, previously known good execution plan for a query using Query Store?

Short answer

You identify the desired plan id from Query Store's history of captured plans for that query, and use the sp_query_store_force_plan stored procedure to instruct SQL Server to always use that specific plan going forward, which is especially useful for quickly stabilizing…

Save SQL Server mock interview
Advertisement
AModel answer
You identify the desired plan id from Query Store's history of captured plans for that query, and use the sp_query_store_force_plan stored procedure to instruct SQL Server to always use that specific plan going forward, which is especially useful for quickly stabilizing performance after a plan regression while you investigate the underlying cause.
EXEC sp_query_store_force_plan
  @query_id = 42,
  @plan_id = 100;
Real-world example
A database administrator forces a previously fast execution plan for a critical report query after Query Store revealed the optimizer had recently switched to a much slower plan, immediately restoring good performance while the root cause is investigated.
Likely follow-ups
  • How do you later unforce a plan once the underlying issue has been fixed?
  • Does forcing a plan guarantee it will always be used regardless of future data changes?
Related topics