Query Store — Complete Guide
Query Store — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of SQL Server Tutorial on Toolliyo Academy.
On this page
SQL Server Tutorial · Lesson 61 of 100
Query Store
SQL basics ✓ → Queries ✓ → Advanced
Advanced · 3 — Procedures · ~10 min · SQL — Advanced SQL Server
What is this?
Query Store records query text, plans, and runtime stats inside the database so you can spot regressions and force a good plan.
Why should you care?
After a deploy, one query suddenly slows — Query Store shows the new plan versus the old one.
See it live — copy this example
Run in SQL Server Management Studio (SSMS) or Azure Data Studio.
ALTER DATABASE DataVerse SET QUERY_STORE = ON (OPERATION_MODE = READ_WRITE);
ALTER DATABASE DataVerse SET QUERY_STORE CLEAR ALL; -- lab only
SELECT TOP (20) q.query_id, qt.query_sql_text, rs.avg_duration
FROM sys.query_store_query q
JOIN sys.query_store_query_text qt ON qt.query_text_id = q.query_text_id
JOIN sys.query_store_plan p ON p.query_id = q.query_id
JOIN sys.query_store_runtime_stats rs ON rs.plan_id = p.plan_id
ORDER BY rs.avg_duration DESC;
What happened?
- Enabling Query Store starts capturing.
- The SELECT finds recent heavy queries by average duration — start of a regression hunt.
Practice next
- Turn Query Store on for your database.
- Run a few ordinary queries.
- Open Query Store reports in SSMS or run the DMV query.
- Set MAX_STORAGE_SIZE_MB thoughtfully.
- Compare wait stats categories in Query Store UI.
Remember
Query Store history of plans + stats. Use it to diagnose regressions. Force plans sparingly.
Post-release regression
DataVerse checkout query flipped plans overnight.
Outcome: Team forced the prior plan, then fixed sniffing properly.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!