Skip to content

Commit 351d659

Browse files
authored
Merge pull request #869 from BrentOzarULTD/sp_BlitzIndex_825
Sp blitzindex 825
2 parents 3d34d07 + 6080279 commit 351d659

File tree

1 file changed

+39
-33
lines changed

1 file changed

+39
-33
lines changed

sp_BlitzIndex.sql

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2503,73 +2503,79 @@ 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)
2515-
INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, URL, details, index_definition,
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)
2514+
INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], 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,
2520+
database_name AS [Database Name],
25212521
N'No indexes use includes' AS finding, 'http://BrentOzar.com/go/IndexFeatures' AS URL,
25222522
N'No indexes use includes' AS details,
2523-
@DatabaseName + N' (Entire database)' AS index_definition,
2523+
database_name + N' (Entire database)' AS index_definition,
25242524
N'' AS secret_columns,
25252525
N'N/A' AS index_usage_summary,
2526-
N'N/A' AS index_size_summary OPTION ( RECOMPILE );
2526+
N'N/A' AS index_size_summary
2527+
FROM #index_includes
2528+
WHERE number_indexes_with_includes = 0
2529+
OPTION ( RECOMPILE );
25272530

25282531
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)
2532+
IF NOT (@Mode = 0)
25302533
INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition,
25312534
secret_columns, index_usage_summary, index_size_summary )
25322535
SELECT 31 AS check_id,
25332536
NULL AS index_sanity_id,
25342537
150 AS Priority,
25352538
N'Feature-Phobic Indexes' AS findings_group,
25362539
N'Borderline: Includes are used in < 3% of indexes' AS findings,
2537-
@DatabaseName AS [Database Name],
2540+
database_name AS [Database Name],
25382541
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,
2542+
N'Only ' + CAST(percent_indexes_with_includes AS NVARCHAR(20)) + '% of indexes have includes' AS details,
25402543
N'Entire database' AS index_definition,
25412544
N'' AS secret_columns,
25422545
N'N/A' AS index_usage_summary,
2543-
N'N/A' AS index_size_summary OPTION ( RECOMPILE );
2546+
N'N/A' AS index_size_summary
2547+
FROM #index_includes
2548+
WHERE number_indexes_with_includes > 0 AND percent_indexes_with_includes <= 3
2549+
OPTION ( RECOMPILE );
25442550

25452551
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;
2548-
2549-
SELECT @count_filtered_indexes=COUNT(*)
2550-
FROM #IndexSanity
2551-
WHERE filter_definition <> '' OPTION ( RECOMPILE );
2552-
2553-
SELECT @count_indexed_views=COUNT(*)
2554-
FROM #IndexSanity AS i
2555-
JOIN #IndexSanitySize AS sz ON i.index_sanity_id = sz.index_sanity_id
2556-
WHERE is_indexed_view = 1 OPTION ( RECOMPILE );
25572552

2558-
IF @count_filtered_indexes = 0 AND @count_indexed_views=0 AND NOT (@GetAllDatabases = 1 OR @Mode = 0)
2553+
IF NOT (@Mode = 0)
25592554
INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition,
25602555
secret_columns, index_usage_summary, index_size_summary )
2561-
SELECT 32 AS check_id,
2556+
SELECT DISTINCT
2557+
32 AS check_id,
25622558
NULL AS index_sanity_id,
25632559
250 AS Priority,
25642560
N'Feature-Phobic Indexes' AS findings_group,
25652561
N'Borderline: No filtered indexes or indexed views exist' AS finding,
2566-
@DatabaseName AS [Database Name],
2562+
i.database_name AS [Database Name],
25672563
N'http://BrentOzar.com/go/IndexFeatures' AS URL,
25682564
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,
2565+
i.database_name + N' (Entire database)' AS index_definition,
25702566
N'' AS secret_columns,
25712567
N'N/A' AS index_usage_summary,
2572-
N'N/A' AS index_size_summary OPTION ( RECOMPILE );
2568+
N'N/A' AS index_size_summary
2569+
FROM #IndexSanity i
2570+
WHERE i.database_name NOT IN (
2571+
SELECT database_name
2572+
FROM #IndexSanity
2573+
WHERE filter_definition <> '' )
2574+
AND i.database_name NOT IN (
2575+
SELECT database_name
2576+
FROM #IndexSanity
2577+
WHERE is_indexed_view = 1 )
2578+
OPTION ( RECOMPILE );
25732579
END;
25742580

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

0 commit comments

Comments
 (0)