SQL Flow Field Guide

Find a plan regression in Query Store

Compare captured plans and runtime measurements over time before recommending a plan correction.

What you are trying to do

A production support report says a query became slower after a release. Use a read-only SQL Server training database where Query Store is already enabled and has representative history. This lab reads recorded metadata and changes no database settings.

Work through it

  1. Run the query and note query IDs, plan IDs, execution count, and weighted average duration for the most recent day.
  2. Choose a query with more than one plan. Compare its duration and logical reads by plan, then open the corresponding actual plan or Query Store plan view.
  3. Check the time interval and capture status. Query Store runtime values are aggregated over intervals, so they are not a per-execution trace.
  4. Use the Query Store reports to compare the regression window with the earlier baseline. Record query text, plan IDs, interval, executions, and resource evidence before proposing a force or code change.
  5. Plan forcing is an operational change. Do not force a plan during this read-only lab. Review Microsoft's Query Store guidance and its wait-stat availability notes. Monitor performance with Query Store

Check your work

The output identifies captured plans and weighted duration for the chosen interval. The investigation records the time range, execution volume, and plan evidence, and distinguishes aggregated Query Store data from a single live execution.

Practice SQL

Open in SQL Flow
SELECT TOP (20) q.query_id, p.plan_id, qt.query_sql_text, SUM(rs.count_executions) AS Executions, SUM(rs.avg_duration * rs.count_executions) / NULLIF(SUM(rs.count_executions), 0) AS WeightedAverageDurationMicroseconds, SUM(rs.avg_logical_io_reads * rs.count_executions) / NULLIF(SUM(rs.count_executions), 0) AS WeightedAverageLogicalReads
FROM sys.query_store_query_text AS qt
INNER JOIN sys.query_store_query AS q ON q.query_text_id = qt.query_text_id
INNER JOIN sys.query_store_plan AS p ON p.query_id = q.query_id
INNER JOIN sys.query_store_runtime_stats AS rs ON rs.plan_id = p.plan_id
INNER JOIN sys.query_store_runtime_stats_interval AS rsi ON rsi.runtime_stats_interval_id = rs.runtime_stats_interval_id
WHERE rsi.end_time >= DATEADD(hour, -24, SYSUTCDATETIME())
GROUP BY q.query_id, p.plan_id, qt.query_sql_text
ORDER BY WeightedAverageDurationMicroseconds DESC;

Knowledge check

Commands in this lesson

Next lesson Trace a live blocking chain