Skip to content

Commit 3e168e6

Browse files
Add @primary_window filter and resource_metrics XML to sp_QuickieStore @find_high_impact
New @primary_window parameter narrows @find_high_impact results to queries whose majority activity falls in a single window. Accepts any prefix of business / off-hours / weekend (case-insensitive, b/o/w is enough). Validated up front: errors out unless @find_high_impact = 1 and the value starts with b, o, or w. Filter is applied in the final dynamic SELECT as WHERE o.primary_window LIKE 'Business%' (or 'Off-hours%' / 'Weekend%'), leveraging the existing primary_window classification with its >50% rule. Queries whose primary_window is 'Spread' are excluded by design. The eight individual total_* columns plus max_dop in the @find_high_impact result set are replaced with a single resource_metrics XML column. Built in Step 6 of the #hi_output insert from #hi_scored using FOR XML PATH(N'metrics'), TYPE with attribute-path aliases — native xml output, no string concatenation. The XML also surfaces the avg/min/max per-execution metrics that #hi_query_stats already computes but that previously weren't projected. Shape: <metrics> <cpu total_ms avg_ms min_ms max_ms/> <duration total_ms avg_ms min_ms max_ms/> <physical_reads total_mb avg_mb min_mb max_mb/> <writes total_mb avg_mb min_mb max_mb/> <memory total_mb avg_mb min_mb max_mb/> <tempdb total_mb avg_mb/> <executions total/> <rows total avg/> <parallelism max_dop/> </metrics> Share columns (cpu_share, duration_share, etc.) are kept as dedicated sortable columns rather than folded into the XML. The underlying total_* and max_dop columns remain in #hi_output storage so debug dumps are unaffected; only the final user-facing SELECT changed. Help text updated: new @primary_window description/valid_inputs/defaults, high_impact_columns section mentions resource_metrics and the filter hint, debug parameter dump includes @primary_window. Smoke-tested on sql2022 against StackOverflow2013: all three bucket filters return only rows in that bucket, XML is well-formed with the expected element/attribute shape. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 7808504 commit 3e168e6

1 file changed

Lines changed: 79 additions & 9 deletions

File tree

sp_QuickieStore/sp_QuickieStore.sql

Lines changed: 79 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ ALTER PROCEDURE
102102
@include_query_hash_totals bit = 0, /*will add an additional column to final output with total resource usage by query hash, may be skewed by query_hash and query_plan_hash bugs with forced plans/plan guides*/
103103
@include_maintenance bit = 0, /*Set this bit to 1 to add maintenance operations such as index creation to the result set*/
104104
@find_high_impact bit = 0, /*finds the vital few queries consuming disproportionate resources across cpu, duration, reads, writes, memory, and executions*/
105+
@primary_window nvarchar(20) = NULL, /*with @find_high_impact, restricts results to queries whose majority activity is in this window: business, off-hours, or weekend*/
105106
@help bit = 0, /*return available parameter details, etc.*/
106107
@debug bit = 0, /*prints dynamic sql, statement length, parameter and variable values, and raw temp table contents*/
107108
@troubleshoot_performance bit = 0, /*set statistics xml on for queries against views*/
@@ -204,6 +205,7 @@ BEGIN
204205
WHEN N'@include_query_hash_totals' THEN N'will add an additional column to final output with total resource usage by query hash, may be skewed by query_hash and query_plan_hash bugs with forced plans/plan guides'
205206
WHEN N'@include_maintenance' THEN N'Set this bit to 1 to add maintenance operations such as index creation to the result set'
206207
WHEN N'@find_high_impact' THEN N'finds the vital few queries consuming disproportionate resources across cpu, duration, reads, writes, memory, and executions'
208+
WHEN N'@primary_window' THEN N'with @find_high_impact, restricts results to queries whose majority activity is in this window (business, off-hours, or weekend)'
207209
WHEN N'@help' THEN 'how you got here'
208210
WHEN N'@debug' THEN 'prints dynamic sql, statement length, parameter and variable values, and raw temp table contents'
209211
WHEN N'@troubleshoot_performance' THEN 'set statistics xml on for queries against views'
@@ -266,6 +268,7 @@ BEGIN
266268
WHEN N'@include_query_hash_totals' THEN N'0 or 1'
267269
WHEN N'@include_maintenance' THEN N'0 or 1'
268270
WHEN N'@find_high_impact' THEN N'0 or 1'
271+
WHEN N'@primary_window' THEN N'business, off-hours, or weekend (any unambiguous prefix works: b, biz, off, overnight, w, wknd, etc.)'
269272
WHEN N'@help' THEN '0 or 1'
270273
WHEN N'@debug' THEN '0 or 1'
271274
WHEN N'@troubleshoot_performance' THEN '0 or 1'
@@ -328,6 +331,7 @@ BEGIN
328331
WHEN N'@include_query_hash_totals' THEN N'0'
329332
WHEN N'@include_maintenance' THEN N'0'
330333
WHEN N'@find_high_impact' THEN N'0'
334+
WHEN N'@primary_window' THEN N'NULL'
331335
WHEN N'@help' THEN '0'
332336
WHEN N'@debug' THEN '0'
333337
WHEN N'@troubleshoot_performance' THEN '0'
@@ -416,6 +420,7 @@ BEGIN
416420
SELECT REPLICATE('-', 100) UNION ALL
417421
SELECT 'primary_window: when this query runs most. Business (during @work_start to @work_end on weekdays),' UNION ALL
418422
SELECT ' Off-hours (weekday nights), Weekend, or Spread (no single window > 50%). Percentage shown.' UNION ALL
423+
SELECT ' Use @primary_window = ''business'' / ''off-hours'' / ''weekend'' to filter to queries whose majority activity falls in that window.' UNION ALL
419424
SELECT REPLICATE('-', 100) UNION ALL
420425
SELECT 'object_name: the stored procedure, function, or trigger this query belongs to, or "Adhoc" for ad hoc SQL' UNION ALL
421426
SELECT 'query_sql_text: representative query text (the most-executed variant for this query_hash)' UNION ALL
@@ -435,6 +440,8 @@ BEGIN
435440
SELECT 'cpu_share, duration_share, physical_reads_share, writes_share, memory_share, executions_share:' UNION ALL
436441
SELECT ' what percentage of the server''s total for that metric this single query_hash consumed.' UNION ALL
437442
SELECT ' This is the 80/20 answer: "this one query is X% of all CPU on the server."' UNION ALL
443+
SELECT 'resource_metrics: clickable XML rollup of total/avg/min/max for cpu, duration, physical reads, writes, memory,' UNION ALL
444+
SELECT ' tempdb, executions, rows, and max DOP. Click the column in SSMS to see the full breakdown.' UNION ALL
438445
SELECT REPLICATE('-', 100) UNION ALL
439446
SELECT 'diagnostics: rule-based signals layered on top of the score:' UNION ALL
440447
SELECT ' wait time (dur/cpu=Nx) - duration far exceeds CPU time, meaning the query spends most of its time waiting' UNION ALL
@@ -778,6 +785,27 @@ BEGIN
778785
RETURN;
779786
END;
780787

788+
/*
789+
@primary_window only applies to the @find_high_impact path, and must
790+
match one of the three bucket labels by case-insensitive prefix: b/o/w
791+
*/
792+
IF @primary_window IS NOT NULL
793+
BEGIN
794+
IF @find_high_impact = 0
795+
BEGIN
796+
RAISERROR('@primary_window only applies when @find_high_impact = 1.', 11, 1) WITH NOWAIT;
797+
RETURN;
798+
END;
799+
800+
IF LOWER(@primary_window) NOT LIKE N'b%'
801+
AND LOWER(@primary_window) NOT LIKE N'o%'
802+
AND LOWER(@primary_window) NOT LIKE N'w%'
803+
BEGIN
804+
RAISERROR('@primary_window must start with b (business), o (off-hours), or w (weekend).', 11, 1) WITH NOWAIT;
805+
RETURN;
806+
END;
807+
END;
808+
781809
/*
782810
These are the tables that we'll use to grab data from query store
783811
It will be fun
@@ -4831,6 +4859,7 @@ BEGIN
48314859
rows_share decimal(5, 1) NULL,
48324860
diagnostics nvarchar(4000) NULL,
48334861
volatile_metrics nvarchar(4000) NULL,
4862+
resource_metrics xml NULL,
48344863
total_cpu_ms decimal(38, 6) NULL,
48354864
total_duration_ms decimal(38, 6) NULL,
48364865
total_physical_reads_mb decimal(38, 6) NULL,
@@ -6187,6 +6216,7 @@ OPTION(RECOMPILE);' + @nc10;
61876216
rows_share,
61886217
diagnostics,
61896218
volatile_metrics,
6219+
resource_metrics,
61906220
total_cpu_ms,
61916221
total_duration_ms,
61926222
total_physical_reads_mb,
@@ -6538,6 +6568,40 @@ OPTION(RECOMPILE);' + @nc10;
65386568
2,
65396569
N''
65406570
),
6571+
resource_metrics =
6572+
(
6573+
SELECT
6574+
[cpu/@total_ms] = s.total_cpu_ms,
6575+
[cpu/@avg_ms] = s.avg_cpu_ms,
6576+
[cpu/@min_ms] = s.min_cpu_ms,
6577+
[cpu/@max_ms] = s.max_cpu_ms,
6578+
[duration/@total_ms] = s.total_duration_ms,
6579+
[duration/@avg_ms] = s.avg_duration_ms,
6580+
[duration/@min_ms] = s.min_duration_ms,
6581+
[duration/@max_ms] = s.max_duration_ms,
6582+
[physical_reads/@total_mb] = s.total_physical_reads_mb,
6583+
[physical_reads/@avg_mb] = s.avg_physical_reads_mb,
6584+
[physical_reads/@min_mb] = s.min_physical_reads_mb,
6585+
[physical_reads/@max_mb] = s.max_physical_reads_mb,
6586+
[writes/@total_mb] = s.total_writes_mb,
6587+
[writes/@avg_mb] = s.avg_writes_mb,
6588+
[writes/@min_mb] = s.min_writes_mb,
6589+
[writes/@max_mb] = s.max_writes_mb,
6590+
[memory/@total_mb] = s.total_memory_mb,
6591+
[memory/@avg_mb] = s.avg_memory_mb,
6592+
[memory/@min_mb] = s.min_memory_mb,
6593+
[memory/@max_mb] = s.max_memory_mb,
6594+
[tempdb/@total_mb] = s.total_tempdb_mb,
6595+
[tempdb/@avg_mb] = s.avg_tempdb_mb,
6596+
[executions/@total] = s.total_executions,
6597+
[rows/@total] = s.total_rows,
6598+
[rows/@avg] = s.avg_rows,
6599+
[parallelism/@max_dop] = s.max_dop
6600+
FOR
6601+
XML
6602+
PATH(N'metrics'),
6603+
TYPE
6604+
),
65416605
s.total_cpu_ms,
65426606
s.total_duration_ms,
65436607
s.total_physical_reads_mb,
@@ -6670,14 +6734,7 @@ SELECT
66706734
o.rows_share,
66716735
o.diagnostics,
66726736
o.volatile_metrics,
6673-
o.total_cpu_ms,
6674-
o.total_duration_ms,
6675-
o.total_physical_reads_mb,
6676-
o.total_writes_mb,
6677-
o.total_memory_mb,
6678-
o.total_tempdb_mb,
6679-
o.total_rows,
6680-
o.max_dop
6737+
o.resource_metrics
66816738
FROM #hi_output AS o
66826739
OUTER APPLY
66836740
(
@@ -6704,7 +6761,18 @@ OUTER APPLY
67046761
) AS qp0
67056762
WHERE qp0.n = 1
67066763
) AS qp
6707-
ORDER BY
6764+
' +
6765+
CASE
6766+
WHEN @primary_window IS NULL
6767+
THEN N''
6768+
WHEN LOWER(@primary_window) LIKE N'b%'
6769+
THEN N'WHERE o.primary_window LIKE N''Business%''' + @nc10
6770+
WHEN LOWER(@primary_window) LIKE N'o%'
6771+
THEN N'WHERE o.primary_window LIKE N''Off-hours%''' + @nc10
6772+
WHEN LOWER(@primary_window) LIKE N'w%'
6773+
THEN N'WHERE o.primary_window LIKE N''Weekend%''' + @nc10
6774+
ELSE N''
6775+
END + N'ORDER BY
67086776
o.impact_score DESC,
67096777
' +
67106778
CASE LOWER(@sort_order)
@@ -14084,6 +14152,8 @@ BEGIN
1408414152
@include_maintenance,
1408514153
find_high_impact =
1408614154
@find_high_impact,
14155+
primary_window =
14156+
@primary_window,
1408714157
help =
1408814158
@help,
1408914159
debug =

0 commit comments

Comments
 (0)