Query Store
Query Store is a flight recorder for your database: it keeps every query’s plans and run-time statistics so you can see what got slow and why.
On this page · 5 min · Advanced
On this page
- Learning objectives
- Explanation
- Step-by-step screens
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Advanced
- Updated
- Nov 26, 2026
Related tutorials
Learning objectives#
- Turn on Query Store
- Find top resource-consuming queries
- Spot a plan regression
- Force a good plan
Explanation#
Query Store is on by default for new databases in SQL Server 2022 and Azure SQL. For older databases: ALTER DATABASE ShopDB SET QUERY_STORE = ON. It saves data inside the database, so it survives restarts and moves with backups.
In SSMS, expand the database → Query Store. Top Resource Consuming Queries shows the queries using the most duration, CPU or reads; Regressed Queries shows queries that got slower than before.
Each query can have several plans (shown as coloured dots over time). When a new plan is slower — a classic after statistics change — select the good plan and click Force Plan. SQL Server keeps using it until you unforce it.
Forcing is a quick fix; the lasting fix is usually an index, a query rewrite or updated statistics.
Step-by-step screens#
Drawings of SQL Server 2022 Setup and SQL Server Management Studio (SSMS 20) with the ShopDB sample database. The red numbers match the numbered actions under each picture; names on your computer will differ.
Loading pictures…
Code example: The same steps in T-SQL#
ALTER DATABASE ShopDB SET QUERY_STORE = ON (OPERATION_MODE = READ_WRITE, QUERY_CAPTURE_MODE = AUTO);
-- Top 5 queries by total duration in the last 24 hours
SELECT TOP (5) q.query_id, p.plan_id, qt.query_sql_text,
SUM(rs.count_executions) AS Runs,
SUM(rs.avg_duration * rs.count_executions) / 1000 AS TotalMs
FROM sys.query_store_runtime_stats rs
JOIN sys.query_store_plan p ON p.plan_id = rs.plan_id
JOIN sys.query_store_query q ON q.query_id = p.query_id
JOIN sys.query_store_query_text qt ON qt.query_text_id = q.query_text_id
WHERE rs.last_execution_time > DATEADD(HOUR, -24, SYSUTCDATETIME())
GROUP BY q.query_id, p.plan_id, qt.query_sql_text
ORDER BY TotalMs DESC;
EXEC sp_query_store_force_plan @query_id = 42, @plan_id = 7;
query_id plan_id query_sql_text Runs TotalMs -------- ------- ---------------------------------------------- ----- ------- 42 9 SELECT ... FROM Orders WHERE Status = @Status 18204 96210 17 3 SELECT ... FROM Products WHERE Name LIKE ... 5011 12880
This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.
Step-by-step explanation#
- Query Store starts recording.
- Runtime stats are joined to plans and query text.
- Query 42 uses the most time — with plan 9.
- Plan 7 was faster before, so it is forced.
Real-world use case#
On Monday the order list became slow. Query Store showed the query switched to a new plan on Sunday after a statistics update; forcing the old plan fixed it in minutes while an index was planned.
Common mistakes#
A forced plan can become wrong as data changes. Review forced plans and fix the root cause.
When it reaches its size limit it stops collecting. Watch the size and the cleanup policy.
Best practices#
- Keep Query Store on
- Check Regressed Queries after deployments
- Force plans only as a stop-gap
- Monitor its size
Remember this#
Query Store = plan + stats history.
Top Resource Consuming / Regressed Queries.
Force Plan as a quick fix.
Practice exercise#
Your task: Unforce plan 7 for query 42.
Show solution
EXEC sp_query_store_unforce_plan @query_id = 42, @plan_id = 7;
Mini quiz#
Where does Query Store keep its data?
Want more? Practise all SQL Server MCQs · SQL Server interview questions
My notes#
Saved in this browser.