Skip to content

Commit 5b444a1

Browse files
committed
Adds warnings for odd query data
Closes BrentOzarULTD#804 Adds warnings for long running queries with low cpu Adds warnings for low cost queries with high cpu
1 parent 08b0668 commit 5b444a1

File tree

1 file changed

+42
-4
lines changed

1 file changed

+42
-4
lines changed

sp_BlitzCache.sql

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ CREATE TABLE ##bou_BlitzCacheProcs (
199199
is_spatial BIT,
200200
index_dml BIT,
201201
table_dml BIT,
202+
long_running_low_cpu BIT,
203+
low_cost_high_cpu BIT,
202204
SetOptions VARCHAR(MAX),
203205
Warnings VARCHAR(MAX)
204206
);
@@ -864,6 +866,8 @@ BEGIN
864866
is_spatial BIT,
865867
index_dml BIT,
866868
table_dml BIT,
869+
long_running_low_cpu BIT,
870+
low_cost_high_cpu BIT,
867871
SetOptions VARCHAR(MAX),
868872
Warnings VARCHAR(MAX)
869873
);
@@ -2751,7 +2755,9 @@ SET frequent_execution = CASE WHEN ExecutionsPerMinute > @execution_threshold
27512755
is_sort_expensive = CASE WHEN QueryPlanCost > (@ctp / 2) AND sort_cost >= QueryPlanCost * .5 THEN 1 END,
27522756
is_remote_query_expensive = CASE WHEN remote_query_cost >= QueryPlanCost * .05 THEN 1 END,
27532757
is_forced_serial = CASE WHEN is_forced_serial = 1 THEN 1 END,
2754-
is_unused_grant = CASE WHEN PercentMemoryGrantUsed <= @memory_grant_warning_percent AND MinGrantKB > @MinMemoryPerQuery THEN 1 END
2758+
is_unused_grant = CASE WHEN PercentMemoryGrantUsed <= @memory_grant_warning_percent AND MinGrantKB > @MinMemoryPerQuery THEN 1 END,
2759+
long_running_low_cpu = CASE WHEN AverageDuration > AverageCPU * 4 THEN 1 END,
2760+
low_cost_high_cpu = CASE WHEN QueryPlanCost < @ctp AND AverageCPU > 500. AND QueryPlanCost * 10 < AverageCPU THEN 1 END
27552761
WHERE SPID = @@SPID
27562762
OPTION (RECOMPILE) ;
27572763

@@ -2845,7 +2851,9 @@ SET Warnings = CASE WHEN QueryPlan IS NULL THEN 'We couldn''t find a plan for
28452851
CASE WHEN is_row_level = 1 THEN ', Row Level Security' ELSE '' END +
28462852
CASE WHEN is_spatial = 1 THEN ', Spatial Index' ELSE '' END +
28472853
CASE WHEN index_dml = 1 THEN ', Index DML' ELSE '' END +
2848-
CASE WHEN table_dml = 1 THEN ', Table DML' ELSE '' END
2854+
CASE WHEN table_dml = 1 THEN ', Table DML' ELSE '' END +
2855+
CASE WHEN low_cost_high_cpu = 1 THEN ', Low Cost High CPU' ELSE '' END +
2856+
CASE WHEN long_running_low_cpu = 1 THEN + 'Long Running With Low CPU' ELSE '' END
28492857
, 2, 200000)
28502858
END
28512859
WHERE SPID = @@SPID
@@ -2907,7 +2915,9 @@ SELECT DISTINCT
29072915
CASE WHEN is_row_level = 1 THEN ', Row Level Security' ELSE '' END +
29082916
CASE WHEN is_spatial = 1 THEN ', Spatial Index' ELSE '' END +
29092917
CASE WHEN index_dml = 1 THEN ', Index DML' ELSE '' END +
2910-
CASE WHEN table_dml = 1 THEN ', Table DML' ELSE '' END
2918+
CASE WHEN table_dml = 1 THEN ', Table DML' ELSE '' END +
2919+
CASE WHEN low_cost_high_cpu = 1 THEN ', Low Cost High CPU' ELSE '' END +
2920+
CASE WHEN long_running_low_cpu = 1 THEN + 'Long Running With Low CPU' ELSE '' END
29112921
, 2, 200000)
29122922
END
29132923
FROM ##bou_BlitzCacheProcs b
@@ -3255,7 +3265,9 @@ BEGIN
32553265
CASE WHEN is_row_level = 1 THEN '', 46'' ELSE '''' END +
32563266
CASE WHEN is_spatial = 1 THEN '', 47'' ELSE '''' END +
32573267
CASE WHEN index_dml = 1 THEN '', 48'' ELSE '''' END +
3258-
CASE WHEN table_dml = 1 THEN '', 49'' ELSE '''' END
3268+
CASE WHEN table_dml = 1 THEN '', 49'' ELSE '''' END +
3269+
CASE WHEN long_running_low_cpu = 1 THEN '', 50'' ELSE '''' END +
3270+
CASE WHEN low_cost_high_cpu = 1 THEN '', 51, '' ELSE '''' END
32593271
, 2, 200000) END AS opserver_warning , ' + @nl ;
32603272
END
32613273

@@ -3955,6 +3967,32 @@ BEGIN
39553967
'No URL yet',
39563968
'This can cause recompiles and stuff.') ;
39573969

3970+
IF EXISTS (SELECT 1/0
3971+
FROM ##bou_BlitzCacheProcs p
3972+
WHERE p.long_running_low_cpu = 1
3973+
AND SPID = @@SPID)
3974+
INSERT INTO ##bou_BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details)
3975+
VALUES (@@SPID,
3976+
50,
3977+
150,
3978+
'Long Running Low CPU',
3979+
'You have a query that runs for much longer than it uses CPU',
3980+
'No URL yet',
3981+
'This can be a sign of blocking, linked servers, or poor client application code (ASYNC_NETWORK_IO).') ;
3982+
3983+
IF EXISTS (SELECT 1/0
3984+
FROM ##bou_BlitzCacheProcs p
3985+
WHERE p.low_cost_high_cpu = 1
3986+
AND SPID = @@SPID)
3987+
INSERT INTO ##bou_BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details)
3988+
VALUES (@@SPID,
3989+
51,
3990+
150,
3991+
'Low Cost Query With High CPU',
3992+
'You have a low cost query that uses a lot of CPU',
3993+
'No URL yet',
3994+
'This can be a sign of functions or Dynamic SQL that calls black-box code.') ;
3995+
39583996
IF EXISTS (SELECT 1/0
39593997
FROM #plan_creation p
39603998
WHERE (p.percent_24 > 0 OR p.percent_4 > 0)

0 commit comments

Comments
 (0)