Query Store and Query Performance Insight
This lesson explains what Query Store captures, how to fix plan regressions and how Query Performance Insight presents top queries.
Learning outcomes
After completing this lesson, you will be able to:
- Describe what Query Store records and its settings.
- Explain how to force a plan or apply a Query Store hint.
- Choose between execution plans, DMVs, Query Store and Query Performance Insight.
What Query Store captures
Query Store persists each query's text, its plans, runtime statistics (duration, CPU, reads, memory) and wait statistics in time intervals, inside the database, so history survives restarts and plan cache evictions. It is on by default in Azure SQL Database and for new databases in SQL Server 2022 and later. Settings include the operation mode (READ_WRITE or READ_ONLY), capture mode (AUTO ignores insignificant ad hoc queries), maximum size and stale query threshold; if it fills up, it switches to read-only and stops capturing.
SELECT TOP (10) q.query_id, p.plan_id, qt.query_sql_text,
SUM(rs.count_executions) AS executions,
AVG(rs.avg_duration) / 1000.0 AS avg_duration_ms
FROM sys.query_store_query AS q
JOIN sys.query_store_query_text AS qt ON qt.query_text_id = q.query_text_id
JOIN sys.query_store_plan AS p ON p.query_id = q.query_id
JOIN sys.query_store_runtime_stats AS rs ON rs.plan_id = p.plan_id
GROUP BY q.query_id, p.plan_id, qt.query_sql_text
ORDER BY avg_duration_ms DESC;Query Store
Fixing regressions
A plan regression occurs when a query gets a new, worse plan, for example after a statistics update or a recompile with unusual parameters. Query Store shows each query's plans over time, and the regressed queries report highlights them. The following steps fix a regression without changing code.
-
Find the query
Use the regressed queries or top resource consumers report, or the catalog views, to find the query ID and its plans.
-
Compare plans
Compare the old and new plans' durations, reads and shapes.
-
Force the good plan
Run
EXEC sp_query_store_force_plan @query_id, @plan_id;so the optimizer reuses the known good plan. -
Or apply a hint
Use
sp_query_store_set_hintsto attach a hint such as OPTION (RECOMPILE) or MAXDOP to the query without editing it. -
Monitor
Check that the forced plan stays effective; unforce it with sp_query_store_unforce_plan when the underlying cause is fixed.
Automatic tuning with FORCE_LAST_GOOD_PLAN performs the force step automatically when it detects a regression and reverts if forcing does not help.
The following diagram shows a regression and its fix over a week.

Query Performance Insight
Query Performance Insight, under Intelligent Performance for an Azure SQL database in the portal, reads Query Store data and charts the top five to twenty queries by CPU, duration or execution count over the last day, week or month, with per-query details and annotations for performance recommendations. It requires Query Store and a few hours of data, and the Reader role is enough to see charts, while viewing query text needs Contributor-level roles such as SQL DB Contributor. It shows only the top queries, so workloads with many small queries need Query Store views or database watcher for a complete picture. The tools are compared in the following table.
| Tool | Best for |
|---|---|
| Execution plan | Understanding why one query is slow |
| DMVs | What is happening right now and since the last restart or cache eviction |
| Query Store | History, regressions, plan forcing and hints |
| Query Performance Insight | A quick portal view of top queries in Azure SQL Database |
Which tool?
Slow since Tuesday
A procedure that ran in 200 ms has taken 9 seconds since Tuesday. No code was deployed. The developer cannot change the procedure until the next release. What should the developer do?
Open Query Store (or Query Performance Insight to spot the query, then Query Store for details), find the procedure's query, compare the plan used since Tuesday with the earlier plan, and force the earlier plan with sp_query_store_force_plan. This restores performance without a code change. Clearing the plan cache might help temporarily but the bad plan can return, and DMVs alone do not keep the history of the earlier plan.
Which procedure forces a known good plan for a query?
Show answer
sp_query_store_force_plan with the query ID and plan ID.
Exam Tips
Remember for the exam
- Regression with history: Query Store; force the good plan with sp_query_store_force_plan.
- Add a hint without changing code: sp_query_store_set_hints.
- Portal view of top queries: Query Performance Insight (needs Query Store).
- Automatic plan correction: FORCE_LAST_GOOD_PLAN.