Find Parameter Sniffing Candidates with Query Store
Finds queries with multiple plans and large duration variance. Diagnoses plan regressions and parameter sniffing problems.
SELECT TOP 20 q.query_id,
COUNT(DISTINCT p.plan_id) AS plan_count,
MIN(rs.avg_duration) / 1000 AS min_avg_ms,
MAX(rs.avg_duration) / 1000 AS max_avg_ms,
MAX(rs.avg_duration) / NULLIF(MIN(rs.avg_duration), 0) AS ratio,
LEFT(qt.query_sql_text, 150) AS query_text
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
GROUP BY q.query_id, qt.query_sql_text
HAVING COUNT(DISTINCT p.plan_id) > 1
ORDER BY ratio DESC;Query Store helps identify queries with high plan variance caused by parameter sniffing; consider `OPTIMIZE FOR` hints.
- This script is designed for read-only diagnostics; it does not alter schema or modify data directly.
- Prior to executing on production systems under high load, verify the target database version (Min. Version: 2016).
- Exercise caution with mutating actions (e.g., terminating sessions or dropping indexes) referenced in the notes section.
Other SQLSERVER Diagnostic Scripts
View All (100)SQL Server Running Requests and Blocking Chain
Shows active requests, wait types, blocking sessions and the running statement together. The first diagnostic query to run during a slowdown.
Top CPU-Consuming Queries in SQL Server
Lists the top 20 queries by total and average CPU time from the plan cache. Pinpoints the source of CPU pressure at query level.
SQL Server Missing Index Recommendations (Missing Index DMVs)
Ranks the optimizer's missing index suggestions by estimated benefit. Merge them with existing indexes before applying.
SQL Server Index Fragmentation with Rebuild/Reorganize Advice
Measures fragmentation on large indexes and recommends rebuild or reorganize. Lets you plan maintenance windows without wasted work.