๐Ÿง  SQL Server Query Store โ€“ Complete Practical Guide

If you are still troubleshooting performance using only execution plans and DMVs, you are working the hard way.

Query Store is like a black box flight recorder for your database.
It captures:

โœ” Query history
โœ” Execution plans
โœ” Performance statistics
โœ” Plan changes over time

This makes performance troubleshooting dramatically easier.


1๏ธโƒฃ What is Query Store?

Query Store is a built-in feature introduced in Microsoft SQL Server 2016.

It automatically stores:

  • Query text
  • Execution plans
  • Runtime statistics
  • Historical performance data

๐Ÿ“Œ Even after SQL Server restart.


2๏ธโƒฃ Why Query Store is Powerful

Without Query Store:

  • You troubleshoot only live queries.
  • Historical performance is lost.

With Query Store:

  • You compare past vs current performance.
  • You detect plan regressions.
  • You force stable plans if needed.

3๏ธโƒฃ Enabling Query Store

ALTER DATABASE MyDatabase
SET QUERY_STORE = ON;

Recommended settings:

ALTER DATABASE MyDatabase
SET QUERY_STORE (
    OPERATION_MODE = READ_WRITE,
    CLEANUP_POLICY = (STALE_QUERY_THRESHOLD_DAYS = 30),
    DATA_FLUSH_INTERVAL_SECONDS = 900
);

๐Ÿ“Œ Always enable it in production databases.


4๏ธโƒฃ Viewing Query Store Data

Using SSMS GUI:

Database โ†’ Query Store โ†’ Top Resource Consuming Queries

Or use query:

SELECT *
FROM sys.query_store_query;

5๏ธโƒฃ Detecting Plan Regression

What is Plan Regression?

When:

  • Same query
  • New execution plan
  • Suddenly becomes slow

Query Store lets you compare:

โœ” Old plan
โœ” New plan
โœ” Performance differences


6๏ธโƒฃ Forcing a Stable Plan

If a new plan is bad:

EXEC sp_query_store_force_plan 
    @query_id = 10,
    @plan_id = 5;

โœ” Forces known good plan
โœ” Quick production fix

โš  Use carefully. Always test first.


7๏ธโƒฃ Finding Top CPU Consuming Queries

SELECT TOP 10
    qsqt.query_sql_text,
    qsr.avg_cpu_time,
    qsr.avg_duration
FROM sys.query_store_query_text qsqt
JOIN sys.query_store_query qsq
    ON qsqt.query_text_id = qsq.query_text_id
JOIN sys.query_store_plan qsp
    ON qsq.query_id = qsp.query_id
JOIN sys.query_store_runtime_stats qsr
    ON qsp.plan_id = qsr.plan_id
ORDER BY qsr.avg_cpu_time DESC;

๐Ÿ“Œ Perfect for performance tuning.


8๏ธโƒฃ Identifying Frequently Failing Queries

Query Store tracks execution stats like:

  • Count
  • Duration
  • Resource usage

You can identify:

โœ” Queries running too often
โœ” Queries consuming too much IO


9๏ธโƒฃ Query Store vs DMVs

FeatureDMVsQuery Store
Historical DataโŒ Lost after restartโœ” Saved
Plan HistoryโŒ Noโœ” Yes
Plan ForcingโŒ Noโœ” Yes

๐Ÿ“Œ Query Store is superior for long-term analysis.


๐Ÿ”Ÿ Real Production Scenario

โŒ Issue

After index rebuild, application became slow.

๐Ÿ” Root Cause

New execution plan generated.

โœ… Fix

  • Compared old vs new plan in Query Store
  • Forced previous stable plan
  • Performance restored immediately

1๏ธโƒฃ1๏ธโƒฃ Best Practices for Query Store

โœ” Enable on all production databases
โœ” Monitor storage size
โœ” Clean up old data
โœ” Avoid forcing too many plans
โœ” Review top resource-consuming queries weekly


1๏ธโƒฃ2๏ธโƒฃ When NOT to Force Plans

โŒ When data distribution changes
โŒ When index structure changes
โŒ When schema changes

๐Ÿ“Œ Forcing wrong plan can worsen performance.


1๏ธโƒฃ3๏ธโƒฃ Monitoring Query Store Size

SELECT current_storage_size_mb,
       max_storage_size_mb
FROM sys.database_query_store_options;

1๏ธโƒฃ4๏ธโƒฃ Query Store Configuration Checklist

โœ” Enabled
โœ” READ_WRITE mode
โœ” Automatic cleanup
โœ” Regular monitoring
โœ” Plan forcing only when required


โœ๏ธ Conclusion

Query Store turns performance tuning from guessing into data-driven decision making.

If you master Query Store, you become:

โœ” Faster at troubleshooting
โœ” More confident in production
โœ” A performance specialist

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top