Skip to content

Instantly share code, notes, and snippets.

@jknopp
Last active May 26, 2021 21:00
Show Gist options
  • Save jknopp/ab6e57801f78f8f58d3a9ddcf0c022fa to your computer and use it in GitHub Desktop.
Save jknopp/ab6e57801f78f8f58d3a9ddcf0c022fa to your computer and use it in GitHub Desktop.
SQL Query Store provides insight on query plan choice and performance.
--query store status
SELECT actual_state_desc, desired_state_desc, current_storage_size_mb,
max_storage_size_mb, readonly_reason
FROM sys.database_query_store_options;
--top 10 bad runners. This gives you the query ID to search for the query in SSMS->Query Store->Tracked Queries. Type in the ID, get details on the query.
SELECT TOP 10
qt.query_text_id,
q.query_id,
p.plan_id,
sum(total_query_wait_time_ms) AS sum_total_wait_ms
FROM sys.query_store_wait_stats ws
JOIN sys.query_store_plan p ON ws.plan_id = p.plan_id
JOIN sys.query_store_query q ON p.query_id = q.query_id
JOIN sys.query_store_query_text qt ON q.query_text_id = qt.query_text_id
GROUP BY qt.query_text_id, q.query_id, p.plan_id
ORDER BY sum_total_wait_ms DESC
--List of all queries in the query store
SELECT Txt.query_text_id, Txt.query_sql_text, Pl.plan_id, Qry.*
FROM sys.query_store_plan AS Pl
INNER JOIN sys.query_store_query AS Qry
ON Pl.query_id = Qry.query_id
INNER JOIN sys.query_store_query_text AS Txt
ON Qry.query_text_id = Txt.query_text_id ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment