HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMinor

How can SQL Server's Query Store Reports show data for minute-length intervals, when "Statistics Collection Interval" is set to 1 hour?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
showqueryhourstoreintervalswhencanstatisticshowdata

Problem

In the database properties under Query Store, the "Statistics Collection Interval" is set to 1 hour. This is also confirmed by querying the view query_store_runtime_stats_interval - all intervals span exactly 1 hour. However, when viewing reports like "Overall Resource Consumption", it is possible to select "Minute" in the dropdown for "Aggregation Size" and it shows data that looks plausible.

I was able to catch a query used in the "Top Resource Consuming Queries Report" and it looks like it displays all plans, that were also executed in the selected 1 minute interval, but the stats still seem to refer to the complete collection interval. So my guess would be to not trust anything for intervals smaller then the collection interval. Am I correct, or do the reports acutally show reliable data?

SELECT TOP (@results_row_count)      
    p.query_id query_id
    , q.object_id object_id
    , ISNULL(OBJECT_NAME(q.object_id),'') object_name
    , qt.query_sql_text query_sql_text
    , ROUND(CONVERT(float, SUM(rs.avg_duration*rs.count_executions))*0.001,2) total_duration
    , SUM(rs.count_executions) count_executions
    , COUNT(distinct p.plan_id) num_plans 
FROM sys.query_store_runtime_stats rs      
JOIN sys.query_store_plan p ON p.plan_id = rs.plan_id      
JOIN sys.query_store_query q ON q.query_id = p.query_id      
JOIN sys.query_store_query_text qt ON q.query_text_id = qt.query_text_id  
WHERE NOT (rs.first_execution_time > @interval_end_time OR rs.last_execution_time = 1  
ORDER BY total_duration DESC

Solution

You can see in the query that what they're doing is catching the stuff from within the interval that has a datetime for first execution or last execution that falls within the more narrow time range of the report. So, it's going to contain some very accurate data. However, if some query was executed within that time frame, but it's first and last execution time falls outside the interval you're interested in, you won't see it. The data stored in Query Store is aggregate only, aggregated by the collection interval. There isn't a second, deeper layer of data or anything like that.

Context

StackExchange Database Administrators Q#332852, answer score: 4

Revisions (0)

No revisions yet.