Advertisement
Advanced SQL Server, Query Store

How can Query Store data be queried directly using its underlying system views for custom analysis beyond the built-in reports?

Short answer

Query Store exposes several system views such as sys.query_store_query, sys.query_store_plan, and sys.query_store_runtime_stats, which you can join together to write fully custom queries analyzing exactly the metrics and time periods you care about, giving you more flexibility…

Save SQL Server mock interview
Advertisement
AModel answer
Query Store exposes several system views such as sys.query_store_query, sys.query_store_plan, and sys.query_store_runtime_stats, which you can join together to write fully custom queries analyzing exactly the metrics and time periods you care about, giving you more flexibility than the built-in reports alone for building specialized dashboards or automated alerts.
SELECT qt.query_sql_text, rs.avg_duration, rs.avg_cpu_time
FROM sys.query_store_query q
JOIN sys.query_store_query_text qt ON q.query_text_id = qt.query_text_id
JOIN sys.query_store_plan p ON q.query_id = p.query_id
JOIN sys.query_store_runtime_stats rs ON p.plan_id = rs.plan_id
ORDER BY rs.avg_duration DESC;
Real-world example
A performance engineering team builds a custom automated alert that queries Query Store's system views directly, notifying the team whenever a query's average duration exceeds a defined threshold, going beyond what the built-in reports alone could provide.
Likely follow-ups
  • What other useful system views does Query Store expose for custom analysis?
  • How often should custom Query Store queries like this be run in an automated monitoring process?
Related topics