Query Store Reference

Query Store is SQL Server's built-in flight recorder for query performance. Several tools in the suite can leverage Query Store for higher-confidence analysis — the Unused Index tool uses it for HIGH confidence detection, and the Expensive Query tool uses it for regression detection. This page covers how to enable, configure, and verify Query Store for use with the suite.

Why Enable Query Store?

Which Suite Tools Use Query Store?

ToolParameterWhat It Does
Unused Index@WithQueryStore = 1Scans Query Store plans for index references. If no cached plan references the index, confidence is elevated to HIGH.
Expensive Query@WithQueryStore = 1Compares recent query performance against historical baseline. Detects regressed queries whose plans changed for the worse.

Both are opt-in (default 0) because Query Store scanning is significantly slower than DMV-only analysis. Enable it when you want the deepest analysis, not for routine fleet runs.

Enable Query Store

ALTER DATABASE [MyDatabase] SET QUERY_STORE = ON;
ALTER DATABASE [MyDatabase] SET QUERY_STORE (
    OPERATION_MODE = READ_WRITE,
    DATA_FLUSH_INTERVAL_SECONDS = 900,
    INTERVAL_LENGTH_MINUTES = 60,
    MAX_STORAGE_SIZE_MB = 1024,
    QUERY_CAPTURE_MODE = AUTO,
    SIZE_BASED_CLEANUP_MODE = AUTO,
    MAX_PLANS_PER_QUERY = 200,
    STALE_QUERY_THRESHOLD_DAYS = 30
);

Recommended Settings

SettingRecommendedDescription
OPERATION_MODEREAD_WRITEActive collection. Use READ_ONLY only for temporary troubleshooting on a production hotspot.
DATA_FLUSH_INTERVAL_SECONDS900How often in-memory data is written to disk (15 minutes). Lower values are more durable but increase I/O.
INTERVAL_LENGTH_MINUTES60Aggregation window for runtime statistics. 60 minutes provides good granularity for trending without excessive storage.
MAX_STORAGE_SIZE_MB1024Maximum disk space for Query Store data. 1 GB is generous for most databases. Monitor usage and adjust as needed.
QUERY_CAPTURE_MODEAUTOCaptures queries with meaningful resource usage. Skips trivial queries that would just waste storage.
SIZE_BASED_CLEANUP_MODEAUTOAutomatically purges the oldest data when approaching MAX_STORAGE_SIZE_MB. Prevents the store from going read-only.
MAX_PLANS_PER_QUERY200Maximum execution plans stored per query. The default of 200 is sufficient for most workloads.
STALE_QUERY_THRESHOLD_DAYS30Purge queries not executed in 30 days. Match this to your analysis window — the Unused Index tool's @MinObservDays should be less than this value.

Enable on All User Databases

Enable Query Store across every user database on a server in one shot:

DECLARE @sql NVARCHAR(MAX) = '';

SELECT @sql = @sql + '
ALTER DATABASE ' + QUOTENAME(name) + ' SET QUERY_STORE = ON;
ALTER DATABASE ' + QUOTENAME(name) + ' SET QUERY_STORE (
    OPERATION_MODE = READ_WRITE,
    DATA_FLUSH_INTERVAL_SECONDS = 900,
    INTERVAL_LENGTH_MINUTES = 60,
    MAX_STORAGE_SIZE_MB = 1024,
    QUERY_CAPTURE_MODE = AUTO,
    SIZE_BASED_CLEANUP_MODE = AUTO,
    MAX_PLANS_PER_QUERY = 200,
    STALE_QUERY_THRESHOLD_DAYS = 30
);
'
FROM sys.databases
WHERE database_id > 4
  AND state = 0
  AND is_query_store_on = 0;

EXEC sp_executesql @sql;

Verify Query Store Status

-- Check which databases have Query Store enabled
SELECT name AS DatabaseName,
       is_query_store_on AS QueryStoreEnabled
FROM sys.databases
WHERE database_id > 4 AND state = 0
ORDER BY name;
-- Detailed status for a specific database
USE [MyDatabase];
SELECT * FROM sys.database_query_store_options;

Monitor Query Store Size

-- Check storage usage across all Query Store-enabled databases
SELECT DB_NAME(database_id) AS DatabaseName,
       actual_state_desc AS State,
       current_storage_size_mb AS CurrentMB,
       max_storage_size_mb AS MaxMB,
       CAST(current_storage_size_mb * 100.0 / max_storage_size_mb AS DECIMAL(5,1)) AS PctUsed
FROM sys.database_query_store_options
WHERE desired_state = 1
ORDER BY current_storage_size_mb DESC;

Watch for: If actual_state_desc shows READ_ONLY when you set READ_WRITE, the store hit its size limit and stopped collecting. Increase MAX_STORAGE_SIZE_MB or ensure SIZE_BASED_CLEANUP_MODE = AUTO is set.

Purge Query Store Data

-- Purge all data for a database (fresh start)
ALTER DATABASE [MyDatabase] SET QUERY_STORE CLEAR;

-- Purge a specific query by ID
EXEC sys.sp_query_store_remove_query @query_id = 12345;

Disable Query Store

ALTER DATABASE [MyDatabase] SET QUERY_STORE = OFF;

Disabling stops collection and removes the data. If you just want to pause collection temporarily, use OPERATION_MODE = READ_ONLY instead — the data is preserved for analysis.