SQL Server · Administration

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.

Advanced 5 min Lesson 40 of 44Examples run on SQL Server 2022 and Azure SQL Database.
Before you start:Execution Plans
0/44 in SQL Server
On this page · 5 min · Advanced

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#

SQLThe same steps in T-SQL Illustrative
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;
Expected output
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#

  1. Query Store starts recording.
  2. Runtime stats are joined to plans and query text.
  3. Query 42 uses the most time — with plan 9.
  4. 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#

Forcing and forgetting

A forced plan can become wrong as data changes. Review forced plans and fix the root cause.

Query Store full and read-only

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
SQL Illustrative
EXEC sp_query_store_unforce_plan @query_id = 42, @plan_id = 7;

Mini quiz#

Query Store quizMultiple choice Question 1 of 1

Where does Query Store keep its data?

Score so far: 0/0

Want more? Practise all SQL Server MCQs · SQL Server interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.