Let’s Get Certified
Module 09 / 16 · Monitor, configure, and optimize database resources

Diagnose a plan regression from workload evidence

Use Query Store history, actual plans and blocking evidence before choosing a change.

Diagnose a plan regression from workload evidence

Work through the decision

A release changes the plan for one order lookup. The dashboard’s mean duration rises, but the new plan also handles a different batch of large customer requests. Can you attribute the regression to the new plan yet?

Check your reasoning

Compare executions of the same query by plan, time interval, parameters and workload. Weight interval averages by execution count. Inspect actual and estimated rows, logical reads and waits. A plan force can be a reversible mitigation when evidence supports it. Validate the fix under the affected workload and preserve the ability to unforce.

Watch the source lesson at 1:45:03

Compare the choices

Query Store
Retain query texts, plans and interval statistics for historical comparison. Check capture state and the period available.
Blocking-chain analysis
Trace waiting requests to the lead blocking transaction and its owner. Assess transaction scope and rollback impact.
Actual execution plan
Inspect measured rows and operators for a representative execution alongside estimates. Look for large misestimates, spills and unnecessary work.
Sargable predicate
Express filters in a form that can use suitable indexes. Validate semantics, types and actual I/O.

Test the decision

If other sessions are queued behind an open transaction, an index or plan force may leave the root problem untouched. Identify the lead blocker and transaction owner. The lab separates a change in plan cost from a change in execution mix.

Read interval-level evidence

Run the read-only example in a database with Query Store enabled and permission to read its performance data. Replace 42 with a query ID observed in your own environment. The result is milliseconds per successful execution for each plan and interval. Compare like periods and parameter populations.

Microsoft Learn · Query Store runtime statistics

Inspect the weighted mean in T-SQL

SELECT p.plan_id, s.runtime_stats_interval_id,
       SUM(s.count_executions) AS executions,
       SUM(s.avg_duration * s.count_executions)
         / NULLIF(SUM(s.count_executions), 0) / 1000.0 AS mean_ms
FROM sys.query_store_plan AS p
JOIN sys.query_store_runtime_stats AS s ON s.plan_id = p.plan_id
WHERE p.query_id = 42 AND s.execution_type = 0
GROUP BY p.plan_id, s.runtime_stats_interval_id
ORDER BY s.runtime_stats_interval_id, p.plan_id;
Check the result

Multiple rows can describe one active interval. Accumulate total duration before dividing by executions and converting microseconds to milliseconds. Retrieve interval start and end times from sys.query_store_runtime_stats_interval to align reporting windows.

Practice the guide skills

  1. A new database needs reliable query history. Watch 1:45:03 · Continue at 1:45:35
    Check your answer

    Configure Query Store capture mode, retention and storage limits. Check its actual state so a full or read-only store does not silently stop collecting fresh history.

  2. A statement ran with two plans yesterday. Watch 1:46:22 · Continue at 1:46:48
    Check your answer

    Compare plan-specific runtime statistics over equivalent intervals and weight by execution count. Examine parameter distribution and workload before forcing a previously successful plan.

  3. Sessions wait on a lock while CPU stays low. Watch 1:48:12 · Continue at 1:48:30
    Check your answer

    Find the blocking chain and lead blocker, its transaction age, isolation level and application owner. Correct transaction scope and assess rollback impact before terminating anything.

  4. Which evidence differentiates a current wait from a long-term average? Watch 1:47:21 · Continue at 1:47:50
    Check your answer

    Sample appropriate request and wait DMVs, record the observation time and possible resets, and compare deltas over equal intervals.

  5. A missing-index suggestion promises a large read saving. Watch 1:52:44
    Check your answer

    Inspect overlapping indexes, key order, includes, storage and write costs. Benchmark the query and affected write workload with representative data.

  6. A date filter applies a function to every indexed timestamp. Watch 1:53:30 · Continue at 1:54:25
    Check your answer

    Test a half-open date range on the base column with matching data types. Compare correctness, logical reads and the actual plan.

  7. An estimated 10 rows becomes 400,000 rows before a join. Watch 1:54:46 · Continue at 1:55:17
    Check your answer

    Compare actual and estimated rows and investigate statistics, skew or parameter sensitivity. Check join choice, spills and memory grant in context.

  8. An Intelligent Insights diagnosis points to a plan change. Watch 1:55:43
    Check your answer

    Correlate the diagnosis with Query Store, time of the deployment and resource metrics. Test whether the candidate improvement survives replay.

Official objectives for this lesson

Lab · Compare two Query Store plans under the same workload mix

Save the script as plans.py and run python3 plans.py. The fixture contains execution counts and mean durations in microseconds, labeled by request class using application telemetry. Predict which part of the observed slowdown will remain if both plans receive the original workload mix.

# query ID, plan ID, request class, executions, average duration (microseconds)
samples = [
    (42, 17, "lookup", 80, 10000), (42, 17, "report", 20, 30000),
    (42, 32, "lookup", 20, 12000), (42, 32, "report", 80, 32000),
    (99, 46, "report", 90, 90000),
]
reference_mix = {"lookup": 0.8, "report": 0.2}

for plan in (17, 32):
    rows = [r for r in samples if r[0] == 42 and r[1] == plan]
    count = sum(r[3] for r in rows)
    observed = sum(r[3] * r[4] for r in rows) / count / 1000
    by_class = {}
    for kind in reference_mix:
        members = [r for r in rows if r[2] == kind]
        by_class[kind] = sum(r[3] * r[4] for r in members) / sum(r[3] for r in members)
    matched = sum(reference_mix[kind] * mean for kind, mean in by_class.items()) / 1000
    print(f"plan {plan}: {count} executions, observed {observed:.1f} ms, matched {matched:.1f} ms")
Check the result
plan 17: 100 executions, observed 14.0 ms, matched 14.0 ms
plan 32: 100 executions, observed 28.0 ms, matched 16.0 ms

The raw average doubles because the second interval serves more reports and each class gets slower. At the original mix the change is 2 ms. Compare plan shapes, reads and waits on representative parameters before choosing a remediation. Change the reference mix to 20% lookups and 80% reports, then explain why the matched difference stays at 2 ms.

Related lesson · Choose a signal and route an alert

Knowledge check

Diagnose a plan regression from workload evidence quiz

Answer 3 questions. Check each answer for an explanation.

Worked example · Diagnose a plan regression from workload evidence