📁 Sangam Dhakal - Portfolio v2.0 - Netscape Navigator 4.0
_
□
×

📚 Interview question

Why might a plan forced in Query Store stop being used, and how would you notice?

Why a Query Store forced plan can silently stop being used, how to detect forcing failures, and how to manage forced plans over time.

Mid-Level Theory Intermediate execution-plans performance plan-forcing query-store sql-server

🎯 Key points

  • Forcing works only while the optimiser can produce that plan
  • Dropped or renamed index, schema change, changed SET options or query text break it
  • Changed text or context settings means a new query_id: the force does not follow
  • force_failure_count and last_force_failure_reason_desc in sys.query_store_plan
  • query_store_plan_forcing_failed extended event; new plan_id appearing
  • Query Store must be READ_WRITE and not full, or nothing is captured
  • Review forced plans after releases and data growth; prefer fixing the root cause

⏱ 30-second answer

Forcing only works while the optimiser can still produce that plan. If an index it uses is dropped or renamed, the query text or SET options change (so it is a different query_id), or a schema change makes the plan invalid, forcing fails and SQL Server quietly compiles a normal plan. I notice through force_failure_count and last_force_failure_reason_desc in sys.query_store_plan, the query_store_plan_forcing_failed extended event, or a new plan_id appearing for the query. I also keep Query Store healthy (READ_WRITE, not full) and review forced plans after releases, because plans and data evolve.

🗣 Full answer (senior)

How forcing works. When a plan is forced in Query Store, the optimiser still compiles the query, but it is constrained to produce the forced plan's shape. If it can't, the force fails and SQL Server uses whatever plan it compiles normally — the query does not error.

Why it stops working.

  • Missing objects: an index the plan uses was dropped, renamed or disabled (NO_INDEX), or a referenced object changed.
  • Schema or plan validity: changes that make the plan shape impossible (NO_PLAN), e.g. a computed column or constraint the plan relied on.
  • Different query identity: forcing is attached to a query_id, which is the query text hash plus context settings. A whitespace change in an ORM-generated query, a different SET ANSI_NULLS / ARITHABORT (SSMS vs ADO.NET defaults), or a new parameter list gives a new query_id with no force on it. That's not a failure — it just isn't the same query any more.
  • Query Store state: if Query Store flips to READ_ONLY because it hit MAX_STORAGE_SIZE_MB, existing forcing still applies, but new data stops being captured, so you lose visibility of regressions and failures.

How I notice.

  1. sys.query_store_plan: is_forced_plan = 1 with force_failure_count > 0 and last_force_failure_reason_desc (e.g. NO_INDEX, NO_PLAN, GENERAL_FAILURE).
  2. The query_store_plan_forcing_failed extended event for alerting.
  3. The Queries With Forced Plans report in SSMS, or a new plan_id collecting executions for the query.
  4. For identity changes, search sys.query_store_query_text for the new text and compare context_settings_id.

Production example. We forced a seek plan to fix a parameter-sniffing regression. A later migration renamed the index to match a naming convention; the force failed with NO_INDEX, the scan plan came back, and p95 went from 30 ms to 2 s. A scheduled check on force_failure_count caught it the next morning; we re-forced and, more importantly, fixed the underlying sniffing with a better index so the force could be removed.

🔍 What the interviewer is checking

Checks operational maturity with Query Store: does the candidate know forcing can fail silently and how to watch it?

Green flags: knows forcing is best-effort; lists index changes, schema changes, SET options and query text; uses force_failure_count / last_force_failure_reason_desc and the extended event; mentions Query Store health and reviewing forces.

Red flags: thinks a forced plan is guaranteed forever; doesn't know what makes a query_id; no monitoring; uses forcing instead of fixing the cause.

📚 Learn in depth

query_id and context settings. Query Store identifies a query by query_hash of the normalised text, the batch it belongs to (batch_sql_handle for ad hoc, or object_id for a module), and context_settings_id (SET options, language, date format). Change any of these and you get a new query_id. This is why a fix forced while testing in SSMS (ARITHABORT ON) may not apply to the application (ADO.NET uses ARITHABORT OFF).

Morally equivalent plans. The forced plan doesn't need to be byte-identical; SQL Server accepts a morally equivalent plan (same operators and shape). That's why some changes don't break forcing but an index rename does.

Query Store hints (SQL Server 2022, Azure SQL). sys.sp_query_store_set_hints applies hints such as OPTION (RECOMPILE) or MAXDOP to a query_id without code changes. They have the same identity rule and their own failure tracking in sys.query_store_query_hints.

Health. Watch actual_state_desc vs desired_state_desc in sys.database_query_store_options, readonly_reason, and storage. Size-based cleanup (SIZE_BASED_CLEANUP_MODE = AUTO) and a sensible QUERY_CAPTURE_MODE = AUTO keep it in READ_WRITE.

Automatic tuning. FORCE_LAST_GOOD_PLAN forces the previous plan when it detects a regression and un-forces if the forced plan is worse or fails — useful, but you still need to see what it did (sys.dm_db_tuning_recommendations).

💻 Code

-- Forced plans that are failing
SELECT  q.query_id,
        p.plan_id,
        p.force_failure_count,
        p.last_force_failure_reason_desc,
        p.last_execution_time,
        qt.query_sql_text
FROM sys.query_store_plan AS p
JOIN sys.query_store_query AS q        ON q.query_id = p.query_id
JOIN sys.query_store_query_text AS qt  ON qt.query_text_id = q.query_text_id
WHERE p.is_forced_plan = 1
  AND p.force_failure_count > 0;

-- Query Store health
SELECT actual_state_desc, desired_state_desc, readonly_reason,
       current_storage_size_mb, max_storage_size_mb
FROM sys.database_query_store_options;

-- Extended event for alerting
CREATE EVENT SESSION QsForcingFailed ON DATABASE
ADD EVENT qds.query_store_plan_forcing_failed
ADD TARGET package0.ring_buffer;
ALTER EVENT SESSION QsForcingFailed ON DATABASE STATE = START;

-- Force / unforce
EXEC sys.sp_query_store_force_plan   @query_id = 42, @plan_id = 7;
EXEC sys.sp_query_store_unforce_plan @query_id = 42, @plan_id = 7;

⚠ Common mistakes

  • Treating a forced plan as a permanent fix and never checking it again.
  • Forcing a plan tested in SSMS and not realising the app has different SET options, so a different query_id.
  • Renaming or rebuilding indexes in a migration without checking forced plans that use them.
  • Letting Query Store fill up and go READ_ONLY, then wondering why regressions aren't visible.
  • Forcing to hide a parameter-sniffing or statistics problem instead of fixing the cause.

🏛 Senior insight: trade-offs

Plan forcing is a tourniquet. It buys time during an incident, but every forced plan is hidden coupling between the database's physical design and a specific query's text. I keep a short list of forced plans with an owner and a reason, check them in the release pipeline (a migration that drops or renames an index used by a forced plan should fail review), alert on forcing failures, and aim to remove each force once the root cause — sniffing, stale statistics, a missing index — is fixed. With ORMs like EF Core, small LINQ changes alter the SQL text, so a force is more fragile than in a stored procedure.

↩ Usually asked after

More in Query Performance

Last updated 23 Nov 2026.

🌐 Sangam's Webring

⏪ Previous 🏠 Main Next ⏩
💬 Chat with me