What you will be able to do
- Explain what Query Store captures - query, plan, runtime stats - and the plan-cache gap it fills.
- Find a regression: two plans for one query in the window where performance dropped.
- Force a plan or apply a Query Store hint to fix a regression without changing application code.
- Name the IQP features built on Query Store - PSP, CE/memory-grant/DOP feedback - and their versions.
The plan cache holds the latest plan and forgets the rest - so when a query regresses, the evidence (what the good plan looked like) is usually already gone. Query Store is the fix: a per-database flight recorder that keeps the history of queries, plans, and runtime statistics in time windows, so a plan change is something you can see instead of infer.
The analogy is a machine’s gauge versus its maintenance logbook. The plan cache is the gauge - it shows only what the engine is doing now and erases whatever it did before. Query Store is the logbook - every plan the query ever ran and how each performed, stamped by interval, so when the machine suddenly runs hot you can flip back and see exactly which entry changed and when. Forcing a plan is clipping the logbook open at the page where it ran well and telling the operator to keep that setting until the real fault is fixed.
What it captures, and the gap it fills
For each query, Query Store retains every plan used over time plus the aggregated runtime statistics per interval - duration, CPU, logical reads, waits. The practical consequence: when a plan changed yesterday and the query is slow today, you do not guess. You open Query Store, see the two plans, see the timestamp where the switch happened, and see which is the regression.
- On by default for new databases from SQL Server 2022, and in Azure SQL Database; off by default on 2016-2019 - enabling it is the first operational step on those versions.
- Captures DML (
SELECT,INSERT,UPDATE,DELETE,MERGE,BULK INSERT) - not DDL. - Cannot be enabled on
masterortempdb. - Since 2017, a wait-stats dimension ties waits to specific queries, not just the instance.
The regression workflow
Find and fix a plan regression in Query Store
Baseline exists
Query Store is ON and has history
If it was not on before the incident, you have no baseline - enable it now for next time
Find the regression
The Regressed Queries view shows queries whose cost rose between intervals
Two plans for one query, with different costs, is the fingerprint
Compare the plans
The good plan vs the bad plan, side by side
The difference tells you the cause - a join change, a scan, a lookup that appeared
Force the good plan
sp_query_store_force_plan or a Query Store hint
Immediate mitigation with no deploy - then go fix the underlying cause
Plan forcing and Query Store hints are the code-free levers. Forcing pins a chosen plan for
a query; a Query Store hint applies a hint (such as RECOMPILE or OPTIMIZE FOR) without editing
the application. Both are mitigation, not cure - the underlying cause (stale statistics,
parameter sensitivity, a schema change) still needs the fix from the earlier lessons. But they are
the difference between a same-day mitigation and an emergency code deploy.
The Intelligent Query Processing it enables
Query Store is also the substrate for the engine’s self-tuning features:
| Feature | Version | What it does on top of Query Store |
|---|---|---|
| CE feedback | 2022 | Detects recurring misestimates and applies model adjustments to future compiles |
| Memory grant feedback | 2019, persisted 2022 | Learns a better grant after a spill or an over-grant, persisted across restarts |
| DOP feedback | 2022 | Identifies queries where parallelism hurts and lowers their DOP |
| PSP optimisation | 2022 | Keeps multiple plans for one parameter-sensitive query and dispatches by value |
Each of these is the engine doing automatically what the earlier lessons taught you to do by hand - which is exactly why they are version-labelled features, not defaults to assume. On 2019 and earlier, or with Query Store off, none of them exists and the manual method is the only one.
What it does not do
- It is not real-time - statistics aggregate per interval; it is a history tool, not a live
monitor. For “what is running right now,” that is
sys.dm_exec_requests. - It costs some overhead - usually modest, but a capture-all policy on a very high-throughput ad-hoc workload adds real work; a custom capture policy is the tuning knob.
- Forcing is not a fix - it stops the bleeding while the underlying cause is repaired; a forced plan left in place past its reason is tomorrow’s regression.
Guided practice
A beginner exercise, worked in a scratch database (never master or tempdb - Query Store cannot
be enabled there). The scenario: create a plan regression on purpose, find it in Query Store, and
pin the good plan without a code change. Work each step and check yourself before moving on.
- Switch the recorder on.
ALTER DATABASE scratch SET QUERY_STORE = ON;- then confirm it is collecting. Check: Query Store is on for your scratch database. On 2016-2019 this is a required first step (off by default); from 2022 new databases have it on. If it was not on before the incident, there is no baseline - the lesson’s first stage is exactly this. - Generate plan history. Create a small table, index the queried column, and run the same
parameterised query a number of times so Query Store accumulates runtime statistics for it.
Check: the query appears in Query Store with at least one plan and aggregated stats -
duration, CPU, logical reads, waits. If nothing was captured, check it was DML (
SELECTqualifies; DDL does not) and that you are looking in the right database. - Force a plan change. Drop the index - or otherwise change the access path - and run the same query again several times. Check: the same query now has two plans in Query Store with different costs - the regression fingerprint. If only one plan exists, the new executions reused the old plan; make the access-path change real and run enough executions to register.
- Read the regression. Open the Regressed Queries view (or the Query Store reports) and compare the two plans side by side, noting the timestamp where the switch happened. Check: you can name what changed between the plans - for example a seek became a scan - rather than just “it got slower”. If the difference does not tell you a cause, revisit the workflow diagram: the plan diff is the diagnosis, not the cost number alone.
- Pin the good plan without a deploy. Use
sp_query_store_force_planon the better plan, re-run the query, and confirm the forced plan is used. Check: performance returns to the good plan’s shape with no application change - the code-free mitigation working as designed. If the workload still uses the bad plan, verify the plan id you forced and that forcing succeeded. - Say what forcing did not do. Unforce the plan, then state in one sentence what still needs a real fix. Check: forcing stopped the bleeding; the underlying cause - the dropped index, stale statistics, or parameter sensitivity - is untouched, and a forced plan left in place past its reason is tomorrow’s regression. If you answered “nothing, it is fixed”, reread “what it does not do”.
Check your understanding
Ordered easy to hard. Answer each before reading the answer; a wrong answer names where to look.
- What does Query Store capture that the plan cache throws away? History: every plan used per query over time plus aggregated runtime statistics per interval - duration, CPU, logical reads, waits. The plan cache keeps the latest plan and forgets the rest, so the evidence for a regression is usually gone without it. Reread the opening if the flight-recorder framing is unclear.
- Where is Query Store on by default, and where can it not run at all?
On by default for new databases from SQL Server 2022 (and in Azure SQL Database); off by default
on 2016-2019, where enabling it is the first operational step. It cannot be enabled on
masterortempdb. If you assumed it was always on, revisit the capture list. - What is the fingerprint of a plan regression in Query Store? Two plans for one query with wildly different costs, with a timestamp where the switch happened - the Regressed Queries view surfaces them between intervals. The plan diff is the cause made visible; if you answered “high duration”, that is the symptom, not the fingerprint.
- Why is plan forcing mitigation rather than cure?
It pins the good plan and stops the bleeding with no deploy - but the cause (stale statistics,
parameter sensitivity, a schema change) still needs the fix from the earlier lessons, and a
forced plan left in place past its reason is tomorrow’s regression. The same is true of a Query
Store hint such as
RECOMPILEorOPTIMIZE FORapplied without a code change. - Which IQP features run on Query Store, and which versions carry them? CE feedback (2022) adjusts the model after recurring misestimates; memory grant feedback (2019, persisted from 2022) relearns grants after spills or over-grants; DOP feedback (2022) lowers parallelism where it hurts; PSP optimisation (2022) keeps multiple plans for a parameter-sensitive query. None exists on 2019 and earlier or with Query Store off - if you presented them as defaults, reread the IQP table; they are version-labelled features, not assumptions.
Section close - the stage project and the debrief
Project: the deliberately slow workload. In a scratch database, build a workload that is slow on
purpose - a skewed status column, a query that ignores the composite key-order rule, and a
commit-per-row insert loop. Then act as the on-call engineer: capture a baseline (Query Store on, a
wait-stats snapshot, the query’s p95 and p99), run the workload, take the wait delta, and name the
top resource. Fix exactly one thing - the index, the batch, or the parameter problem, your choice
but one only - re-run, and prove the p99 moved. The deliverable is one page: the baseline waits, the
single change, and the before-and-after tail. Walkthrough: baseline first, because nothing after it
counts as evidence without it; let the wait delta choose the fix rather than your favourite lesson;
change one variable so the improvement is attributable. Where people get stuck: fixing three things
at once and being unable to say which moved the p99; comparing averages instead of tails and
declaring victory on a number no user feels; and diagnosing off a single cumulative wait snapshot,
which names the noise accumulated since boot rather than the window’s problem.
Debrief. Having finished this section you can measure latency honestly - tails over averages - take a wait delta and turn it into a named resource, design a composite index instead of collecting them, tell stale statistics from a structural misestimate, recognise parameter sensitivity, reason about what a commit costs and when minimal logging applies, separate a lock wait from a latch wait from an I/O wait, read tempdb’s three consumers, and use Query Store to pin a regression to a plan change. The gaps that usually remain even when it feels understood: you can name the wait type but cannot yet cost the fix, which takes production exposure a scratch database cannot give; the estimated-versus-actual rows skill needs more repetitions than one lesson provides; and everything here was one engine’s internals - the moment data has to move between systems, a different discipline begins. Next session: Data Pipeline Engineering, starting with “The pipeline lifecycle - extraction to trusted destination” - the lesson where the unit of work stops being a query and becomes a repeatable, recoverable process.
Where SQL Server sits
How this component relates to the rest of the stack. Items without a link are not covered here yet.
Deployed with
Commonly run alongside
- Windows Failover Clustering