Skip to content

Commit ff3bc32

Browse files
Remove sp_QuickieStore wait-stats per-interval TOP (5) pre-filter
The CROSS APPLY that joined to sys.query_store_wait_stats had: SELECT TOP (5) qsws.* FROM sys.query_store_wait_stats AS qsws WHERE qsws.runtime_stats_interval_id = qsrs.runtime_stats_interval_id AND qsws.plan_id = qsrs.plan_id ... ORDER BY qsws.avg_query_wait_time_ms DESC so for each (interval, plan) it kept only the 5 wait categories with the highest per-interval avg wait time and discarded the rest. The outer query then GROUP BYs (plan_id, wait_category_desc) and sums across intervals. The net effect: a wait category ranked 6+ in one interval but present in another silently loses the first interval's contribution, making the same plan's totals inconsistent run-to-run depending on which intervals it executed in. Removed the TOP (and the now-unused ORDER BY) so every captured wait category contributes to the outer aggregation. QS caps wait categories to a small set per (interval, plan), so removing the TOP does not blow up row counts. Verified sp_QuickieStore installs clean and runs without error against PerformanceMonitor on SQL Server 2022 with @wait_filter. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent fc16d14 commit ff3bc32

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

sp_QuickieStore/sp_QuickieStore.sql

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10863,15 +10863,24 @@ FROM
1086310863
FROM #query_store_runtime_stats AS qsrs
1086410864
CROSS APPLY
1086510865
(
10866-
SELECT TOP (5)
10866+
/*
10867+
Pull every wait category captured for this (interval, plan).
10868+
The previous TOP (5) ORDER BY avg_query_wait_time_ms DESC here
10869+
dropped wait categories ranked 6+ per interval before the outer
10870+
GROUP BY ran, so a category that was (say) 6th worst in one
10871+
interval but 2nd worst in another would silently have the first
10872+
interval''s contribution missing from its totals. The outer
10873+
aggregation groups by (plan_id, wait_category_desc) and the
10874+
number of wait categories per interval is capped by QS at a
10875+
small set, so removing the TOP does not explode row counts.
10876+
*/
10877+
SELECT
1086710878
qsws.*
1086810879
FROM ' + @database_name_quoted + N'.sys.query_store_wait_stats AS qsws
1086910880
WHERE qsws.runtime_stats_interval_id = qsrs.runtime_stats_interval_id
1087010881
AND qsws.plan_id = qsrs.plan_id
1087110882
AND qsws.wait_category > 0
1087210883
AND qsws.min_query_wait_time_ms > 0
10873-
ORDER BY
10874-
qsws.avg_query_wait_time_ms DESC
1087510884
) AS qsws
1087610885
WHERE qsrs.database_id = @database_id
1087710886
) AS qsws_with_lasts

0 commit comments

Comments
 (0)