Skip to content

Commit b5aa3c0

Browse files
committed
1 parent 6cd5c8d commit b5aa3c0

File tree

1 file changed

+51
-3
lines changed

1 file changed

+51
-3
lines changed

sp_BlitzCache.sql

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ CREATE TABLE ##bou_BlitzCacheProcs (
195195
index_ops AS (index_insert_count + index_update_count + index_delete_count +
196196
cx_insert_count + cx_update_count + cx_delete_count +
197197
table_insert_count + table_update_count + table_delete_count),
198+
is_large_scan BIT,
198199
SetOptions VARCHAR(MAX),
199200
Warnings VARCHAR(MAX)
200201
);
@@ -830,6 +831,7 @@ BEGIN
830831
index_ops AS (index_insert_count + index_update_count + index_delete_count +
831832
cx_insert_count + cx_update_count + cx_delete_count +
832833
table_insert_count + table_update_count + table_delete_count),
834+
is_large_scan BIT,
833835
SetOptions VARCHAR(MAX),
834836
Warnings VARCHAR(MAX)
835837
);
@@ -2229,6 +2231,37 @@ BEGIN
22292231
OPTION (RECOMPILE) ;
22302232
END ;
22312233

2234+
IF (@v = 13 AND @build >= 4001)
2235+
BEGIN
2236+
RAISERROR(N'Using 2016 SP1 estimated rows read to detect inefficient scans', 0, 1) WITH NOWAIT;
2237+
2238+
WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p),
2239+
er AS (
2240+
SELECT
2241+
r.SqlHandle,
2242+
c.n.value('@EstimateRows', 'FLOAT') AS estimated_rows,
2243+
c.n.value('@EstimatedRowsRead', 'FLOAT') AS estimated_rows_read
2244+
FROM #relop AS r
2245+
CROSS APPLY r.relop.nodes('//p:RelOp') c(n)
2246+
WHERE c.n.exist('@EstimatedRowsRead') = 1
2247+
), er2 AS (
2248+
SELECT *,
2249+
CASE WHEN er.estimated_rows_read >= 100000
2250+
AND er.estimated_rows * 10 < er.estimated_rows_read
2251+
THEN 1
2252+
ELSE 0
2253+
END AS is_large_scan
2254+
FROM er
2255+
)
2256+
UPDATE b
2257+
SET b.is_large_scan = er2.is_large_scan
2258+
FROM ##bou_BlitzCacheProcs b
2259+
JOIN er2
2260+
ON b.SqlHandle = er2.SqlHandle
2261+
WHERE er2.is_large_scan = 1
2262+
OPTION (RECOMPILE);
2263+
END
2264+
22322265
/* END Testing using XML nodes to speed up processing */
22332266
RAISERROR(N'Gathering additional plan level information', 0, 1) WITH NOWAIT;
22342267
WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p)
@@ -2516,7 +2549,8 @@ SET Warnings = CASE WHEN QueryPlan IS NULL THEN 'We couldn''t find a plan for
25162549
CASE WHEN is_computed_scalar = 1 THEN ', Computed Column UDF ' ELSE '' END +
25172550
CASE WHEN is_sort_expensive = 1 THEN ', Expensive Sort' ELSE '' END +
25182551
CASE WHEN is_computed_filter = 1 THEN ', Filter UDF' ELSE '' END +
2519-
CASE WHEN index_ops >= 5 THEN ', ' + CONVERT(VARCHAR(10), index_ops) + ' Indexes Modified' ELSE '' END
2552+
CASE WHEN index_ops >= 5 THEN ', ' + CONVERT(VARCHAR(10), index_ops) + ' Indexes Modified' ELSE '' END +
2553+
CASE WHEN is_large_scan = 1 THEN ', Inefficient Scan' ELSE '' END
25202554
, 2, 200000)
25212555
END
25222556
WHERE SPID = @@SPID
@@ -2844,7 +2878,8 @@ BEGIN
28442878
CASE WHEN is_computed_scalar = 1 THEN '', 42 '' ELSE '' END +
28452879
CASE WHEN is_sort_expensive = 1 THEN '', 43'' ELSE '''' END +
28462880
CASE WHEN is_computed_filter = 1 THEN '', 44'' ELSE '''' END +
2847-
CASE WHEN index_ops >= 5 THEN '', 45'' ELSE '''' END
2881+
CASE WHEN index_ops >= 5 THEN '', 45'' ELSE '''' END +
2882+
CASE WHEN is_large_scan = 1 THEN '', 46'' ELSE '''' END +
28482883
, 2, 200000) END AS opserver_warning , ' + @nl ;
28492884
END
28502885

@@ -3489,7 +3524,20 @@ BEGIN
34893524
'Write Queries Are Hitting >= 5 Indexes',
34903525
'No URL yet',
34913526
'This can cause lots of hidden I/O -- Run sp_BLitzIndex for more information.') ;
3492-
3527+
3528+
IF EXISTS (SELECT 1/0
3529+
FROM ##bou_BlitzCacheProcs p
3530+
WHERE p.is_large_scan = 1
3531+
AND SPID = @@SPID)
3532+
INSERT INTO ##bou_BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details)
3533+
VALUES (@@SPID,
3534+
46,
3535+
100,
3536+
'Inefficient Scan',
3537+
'More than 10x rows read than returned',
3538+
'No URL yet',
3539+
'This looks for scans of >100k rows where the output is < 10x the scan.') ;
3540+
34933541
IF EXISTS (SELECT 1/0
34943542
FROM #plan_creation p
34953543
WHERE (p.percent_24 > 0 OR p.percent_4 > 0)

0 commit comments

Comments
 (0)