Skip to content

Commit 45ddfb2

Browse files
committed
Changes checks for index included columns, filtered indexes, and indexed views to work with @GetAllDatabases = 1
1 parent 3d34d07 commit 45ddfb2

File tree

1 file changed

+43
-27
lines changed

1 file changed

+43
-27
lines changed

sp_BlitzIndex.sql

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2503,73 +2503,89 @@ BEGIN;
25032503
https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/issues/825
25042504
*/
25052505

2506-
DECLARE @number_indexes_with_includes INT;
2507-
DECLARE @percent_indexes_with_includes NUMERIC(10, 1);
2508-
2509-
SELECT @number_indexes_with_includes = SUM(CASE WHEN count_included_columns > 0 THEN 1 ELSE 0 END),
2510-
@percent_indexes_with_includes = 100.*
2511-
SUM(CASE WHEN count_included_columns > 0 THEN 1 ELSE 0 END) / ( 1.0 * COUNT(*) )
2512-
FROM #IndexSanity;
2513-
2514-
IF @number_indexes_with_includes = 0 AND NOT (@GetAllDatabases = 1 OR @Mode = 0)
2506+
SELECT database_name,
2507+
SUM(CASE WHEN count_included_columns > 0 THEN 1 ELSE 0 END) AS number_indexes_with_includes,
2508+
100.* SUM(CASE WHEN count_included_columns > 0 THEN 1 ELSE 0 END) / ( 1.0 * COUNT(*) ) AS percent_indexes_with_includes
2509+
INTO #index_includes
2510+
FROM #IndexSanity
2511+
GROUP BY database_name;
2512+
2513+
IF NOT (@Mode = 0)
25152514
INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, URL, details, index_definition,
25162515
secret_columns, index_usage_summary, index_size_summary )
2517-
SELECT 30 AS check_id,
2516+
SELECT 30 AS check_id,
25182517
NULL AS index_sanity_id,
25192518
250 AS Priority,
25202519
N'Feature-Phobic Indexes' AS findings_group,
25212520
N'No indexes use includes' AS finding, 'http://BrentOzar.com/go/IndexFeatures' AS URL,
25222521
N'No indexes use includes' AS details,
2523-
@DatabaseName + N' (Entire database)' AS index_definition,
2522+
database_name + N' (Entire database)' AS index_definition,
25242523
N'' AS secret_columns,
25252524
N'N/A' AS index_usage_summary,
2526-
N'N/A' AS index_size_summary OPTION ( RECOMPILE );
2525+
N'N/A' AS index_size_summary
2526+
FROM #index_includes
2527+
WHERE number_indexes_with_includes = 0
2528+
OPTION ( RECOMPILE );
25272529

25282530
RAISERROR(N'check_id 31: < 3 percent of indexes have includes', 0,1) WITH NOWAIT;
2529-
IF @percent_indexes_with_includes <= 3 AND @number_indexes_with_includes > 0 AND NOT (@GetAllDatabases = 1 OR @Mode = 0)
2531+
IF NOT (@Mode = 0)
25302532
INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition,
25312533
secret_columns, index_usage_summary, index_size_summary )
25322534
SELECT 31 AS check_id,
25332535
NULL AS index_sanity_id,
25342536
150 AS Priority,
25352537
N'Feature-Phobic Indexes' AS findings_group,
25362538
N'Borderline: Includes are used in < 3% of indexes' AS findings,
2537-
@DatabaseName AS [Database Name],
2539+
database_name AS [Database Name],
25382540
N'http://BrentOzar.com/go/IndexFeatures' AS URL,
2539-
N'Only ' + CAST(@percent_indexes_with_includes AS NVARCHAR(10)) + '% of indexes have includes' AS details,
2541+
N'Only ' + CAST(percent_indexes_with_includes AS NVARCHAR(20)) + '% of indexes have includes' AS details,
25402542
N'Entire database' AS index_definition,
25412543
N'' AS secret_columns,
25422544
N'N/A' AS index_usage_summary,
2543-
N'N/A' AS index_size_summary OPTION ( RECOMPILE );
2545+
N'N/A' AS index_size_summary
2546+
FROM #index_includes
2547+
WHERE number_indexes_with_includes > 0 AND percent_indexes_with_includes <= 3
2548+
OPTION ( RECOMPILE );
25442549

25452550
RAISERROR(N'check_id 32: filtered indexes and indexed views', 0,1) WITH NOWAIT;
2546-
DECLARE @count_filtered_indexes INT;
2547-
DECLARE @count_indexed_views INT;
25482551

2549-
SELECT @count_filtered_indexes=COUNT(*)
2552+
SELECT database_name,
2553+
COUNT(*) AS count_filtered_indexes
2554+
INTO #filtered_index_count
25502555
FROM #IndexSanity
2551-
WHERE filter_definition <> '' OPTION ( RECOMPILE );
2556+
WHERE filter_definition <> ''
2557+
GROUP BY database_name
2558+
OPTION ( RECOMPILE );
25522559

2553-
SELECT @count_indexed_views=COUNT(*)
2560+
SELECT i.database_name,
2561+
COUNT(*) AS count_indexed_views
2562+
INTO #indexed_view_count
25542563
FROM #IndexSanity AS i
25552564
JOIN #IndexSanitySize AS sz ON i.index_sanity_id = sz.index_sanity_id
2556-
WHERE is_indexed_view = 1 OPTION ( RECOMPILE );
2565+
WHERE is_indexed_view = 1
2566+
GROUP BY i.database_name
2567+
OPTION ( RECOMPILE );
25572568

2558-
IF @count_filtered_indexes = 0 AND @count_indexed_views=0 AND NOT (@GetAllDatabases = 1 OR @Mode = 0)
2569+
IF NOT (@Mode = 0)
25592570
INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition,
25602571
secret_columns, index_usage_summary, index_size_summary )
2561-
SELECT 32 AS check_id,
2572+
SELECT 32 AS check_id,
25622573
NULL AS index_sanity_id,
25632574
250 AS Priority,
25642575
N'Feature-Phobic Indexes' AS findings_group,
25652576
N'Borderline: No filtered indexes or indexed views exist' AS finding,
2566-
@DatabaseName AS [Database Name],
2577+
fic.database_name AS [Database Name],
25672578
N'http://BrentOzar.com/go/IndexFeatures' AS URL,
25682579
N'These are NOT always needed-- but do you know when you would use them?' AS details,
2569-
@DatabaseName + N' (Entire database)' AS index_definition,
2580+
fic.database_name + N' (Entire database)' AS index_definition,
25702581
N'' AS secret_columns,
25712582
N'N/A' AS index_usage_summary,
2572-
N'N/A' AS index_size_summary OPTION ( RECOMPILE );
2583+
N'N/A' AS index_size_summary
2584+
FROM #filtered_index_count AS fic
2585+
JOIN #indexed_view_count AS ivc
2586+
ON ivc.database_name = fic.database_name
2587+
WHERE count_filtered_indexes = 0 AND count_indexed_views=0
2588+
OPTION ( RECOMPILE );
25732589
END;
25742590

25752591
RAISERROR(N'check_id 33: Potential filtered indexes based on column names.', 0,1) WITH NOWAIT;

0 commit comments

Comments
 (0)