Skip to content

Commit d45b346

Browse files
Merge pull request #748 from erikdarlingdata/dev
Merge dev to main: sp_QuickieCache fixes
2 parents 8f8b4de + 1af00df commit d45b346

1 file changed

Lines changed: 45 additions & 1 deletion

File tree

sp_QuickieCache/sp_QuickieCache.sql

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ ALTER PROCEDURE
1212
dbo.sp_QuickieCache
1313
(
1414
@top integer = 10, /*candidates per metric dimension before dedup*/
15+
@sort_order varchar(20) = 'cpu', /*secondary sort after impact_score: cpu, duration, reads, writes, memory, spills, executions*/
1516
@database_name sysname = NULL, /*filter to a specific database*/
1617
@start_date datetime = NULL, /*only include plans created after this date*/
1718
@end_date datetime = NULL, /*only include plans created before this date*/
@@ -69,6 +70,8 @@ BEGIN
6970
CASE ap.name
7071
WHEN N'@top'
7172
THEN N'Number of candidate queries per metric dimension (cpu, reads, etc.) before dedup. Default: 10.'
73+
WHEN N'@sort_order'
74+
THEN N'Secondary sort after impact_score. Options: cpu, duration, reads, writes, memory, spills, executions. Default: cpu.'
7275
WHEN N'@database_name'
7376
THEN N'Filter to a specific database. Default: NULL (all user databases).'
7477
WHEN N'@start_date'
@@ -183,6 +186,19 @@ BEGIN
183186
RETURN;
184187
END;
185188

189+
SELECT
190+
@sort_order = LOWER(LTRIM(RTRIM(@sort_order)));
191+
192+
IF @sort_order NOT IN
193+
(
194+
'cpu', 'duration', 'reads', 'writes',
195+
'memory', 'spills', 'executions'
196+
)
197+
BEGIN
198+
RAISERROR(N'@sort_order must be one of: cpu, duration, reads, writes, memory, spills, executions.', 16, 1) WITH NOWAIT;
199+
RETURN;
200+
END;
201+
186202
/*
187203
╔══════════════════════════════════════════════════╗
188204
║ Plan cache health analysis ║
@@ -248,6 +264,15 @@ BEGIN
248264
),
249265
@oldest_plan_date = MIN(qs.creation_time)
250266
FROM sys.dm_exec_query_stats AS qs
267+
CROSS APPLY
268+
(
269+
SELECT TOP (1)
270+
value = pa.value
271+
FROM sys.dm_exec_plan_attributes(qs.plan_handle) AS pa
272+
WHERE pa.attribute = N'dbid'
273+
) AS pa
274+
WHERE 1 = 1
275+
AND (@database_id IS NULL OR pa.value = @database_id)
251276
OPTION(RECOMPILE);
252277

253278
IF @total_plans > 0
@@ -387,6 +412,7 @@ BEGIN
387412
WHERE pa.value IS NOT NULL
388413
AND CONVERT(integer, pa.value) NOT IN (1, 2, 3, 4)
389414
AND CONVERT(integer, pa.value) < 32761
415+
AND (@database_id IS NULL OR CONVERT(integer, pa.value) = @database_id)
390416
GROUP BY
391417
pa.value
392418
) AS x
@@ -413,7 +439,15 @@ BEGIN
413439
SELECT
414440
plan_count = COUNT_BIG(*)
415441
FROM sys.dm_exec_query_stats AS qs
442+
CROSS APPLY
443+
(
444+
SELECT TOP (1)
445+
value = pa.value
446+
FROM sys.dm_exec_plan_attributes(qs.plan_handle) AS pa
447+
WHERE pa.attribute = N'dbid'
448+
) AS pa
416449
WHERE qs.query_hash <> 0x0000000000000000
450+
AND (@database_id IS NULL OR CONVERT(integer, pa.value) = @database_id)
417451
GROUP BY
418452
qs.query_hash
419453
HAVING
@@ -487,6 +521,7 @@ BEGIN
487521
WHERE pa.value IS NOT NULL
488522
AND CONVERT(integer, pa.value) NOT IN (1, 2, 3, 4)
489523
AND CONVERT(integer, pa.value) < 32761
524+
AND (@database_id IS NULL OR CONVERT(integer, pa.value) = @database_id)
490525
GROUP BY
491526
pa.value
492527
ORDER BY
@@ -1921,7 +1956,16 @@ OPTION(RECOMPILE, MAXDOP 1);';
19211956
) AS qp
19221957
WHERE s.impact_score >= @impact_threshold
19231958
ORDER BY
1924-
s.impact_score DESC
1959+
s.impact_score DESC,
1960+
CASE @sort_order
1961+
WHEN 'cpu' THEN s.cpu_share
1962+
WHEN 'duration' THEN s.duration_share
1963+
WHEN 'reads' THEN s.reads_share
1964+
WHEN 'writes' THEN s.writes_share
1965+
WHEN 'memory' THEN s.grant_share
1966+
WHEN 'spills' THEN s.spills_share
1967+
WHEN 'executions' THEN s.executions_share
1968+
END DESC
19251969
OPTION(RECOMPILE);
19261970

19271971
/*

0 commit comments

Comments
 (0)