@@ -5195,28 +5195,6 @@ BEGIN
51955195 RAISERROR (N ' @Mode=4, running rules for priorities 101+.' , 0 ,1 ) WITH NOWAIT ;
51965196
51975197 RAISERROR (N ' check_id 21: More Than 5 Percent NC Indexes Are Unused' , 0 ,1 ) WITH NOWAIT ;
5198- DECLARE @percent_NC_indexes_unused NUMERIC (29 ,1 );
5199- DECLARE @NC_indexes_unused_reserved_MB NUMERIC (29 ,1 );
5200-
5201- SELECT @percent_NC_indexes_unused = ( 100 .00 * SUM (CASE
5202- WHEN total_reads = 0
5203- THEN 1
5204- ELSE 0
5205- END ) ) / COUNT (* ),
5206- @NC_indexes_unused_reserved_MB = SUM (CASE
5207- WHEN total_reads = 0
5208- THEN sz .total_reserved_MB
5209- ELSE 0
5210- END )
5211- FROM #IndexSanity i
5212- JOIN #IndexSanitySize sz ON i .index_sanity_id = sz .index_sanity_id
5213- WHERE index_id NOT IN ( 0 , 1 )
5214- AND i .is_unique = 0
5215- /* Skipping tables created in the last week, or modified in past 2 days*/
5216- AND i .create_date < DATEADD (dd,- 7 ,GETDATE ())
5217- AND i .modify_date < DATEADD (dd,- 2 ,GETDATE ())
5218- OPTION ( RECOMPILE );
5219- IF @percent_NC_indexes_unused >= 5
52205198 INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL , details, index_definition,
52215199 secret_columns, index_usage_summary, index_size_summary )
52225200 SELECT 21 AS check_id,
@@ -5226,8 +5204,8 @@ BEGIN
52265204 N ' More Than 5 Percent NC Indexes Are Unused' AS finding,
52275205 [database_name] AS [Database Name],
52285206 N ' https://www.brentozar.com/go/IndexHoarder' AS URL ,
5229- CAST (@ percent_NC_indexes_unused AS NVARCHAR (30 )) + N ' percent NC indexes (' + CAST (COUNT (* ) AS NVARCHAR (10 )) + N ' ) unused. ' +
5230- N ' These take up ' + CAST (@ NC_indexes_unused_reserved_MB AS NVARCHAR (30 )) + N ' MB of space.' AS details,
5207+ CAST (MAX ( perc . percent_NC_indexes_unused ) AS NVARCHAR (30 )) + N ' percent NC indexes (' + CAST (COUNT (* ) AS NVARCHAR (10 )) + N ' ) unused. ' +
5208+ N ' These take up ' + CAST (MAX ( perc . NC_indexes_unused_reserved_MB ) AS NVARCHAR (30 )) + N ' MB of space.' AS details,
52315209 i .database_name + ' (' + CAST (COUNT (* ) AS NVARCHAR (30 )) + N ' indexes)' AS index_definition,
52325210 ' ' AS secret_columns,
52335211 CAST (SUM (total_reads) AS NVARCHAR (256 )) + N ' reads (ALL); '
@@ -5242,14 +5220,27 @@ BEGIN
52425220 END AS index_size_summary
52435221 FROM #IndexSanity i
52445222 JOIN #IndexSanitySize sz ON i .index_sanity_id = sz .index_sanity_id
5223+ JOIN (
5224+ SELECT i .database_name ,
5225+ CAST ((100 .00 * SUM (CASE WHEN total_reads = 0 THEN 1 ELSE 0 END )) / COUNT (* ) AS NUMERIC (29 ,1 )) AS percent_NC_indexes_unused,
5226+ CAST (SUM (CASE WHEN total_reads = 0 THEN sz .total_reserved_MB ELSE 0 END ) AS NUMERIC (29 ,1 )) AS NC_indexes_unused_reserved_MB
5227+ FROM #IndexSanity i
5228+ JOIN #IndexSanitySize sz ON i .index_sanity_id = sz .index_sanity_id
5229+ WHERE index_id NOT IN ( 0 , 1 )
5230+ AND i .is_unique = 0
5231+ /* Skipping tables created in the last week, or modified in past 2 days*/
5232+ AND i .create_date < DATEADD (dd,- 7 ,GETDATE ())
5233+ AND i .modify_date < DATEADD (dd,- 2 ,GETDATE ())
5234+ GROUP BY i .database_name
5235+ ) AS perc ON i .database_name = perc .database_name
52455236 WHERE index_id NOT IN ( 0 , 1 )
52465237 AND i .is_unique = 0
52475238 AND total_reads = 0
52485239 /* Skipping tables created in the last week, or modified in past 2 days*/
52495240 AND i .create_date < DATEADD (dd,- 7 ,GETDATE ())
52505241 AND i .modify_date < DATEADD (dd,- 2 ,GETDATE ())
5251- AND sz . total_reserved_MB >= CASE WHEN (@GetAllDatabases = 1 OR @Mode = 0 ) THEN @ThresholdMB ELSE sz . total_reserved_MB END
5252- GROUP BY i .database_name
5242+ AND perc . percent_NC_indexes_unused >= 5
5243+ GROUP BY i .database_name
52535244 OPTION ( RECOMPILE );
52545245
52555246 RAISERROR (N ' check_id 23: Indexes with 7 or more columns. (Borderline)' , 0 ,1 ) WITH NOWAIT ;
@@ -5358,6 +5349,7 @@ BEGIN
53585349 WHERE i .index_id IN (1 ,0 )
53595350 AND cc .non_nullable_columns < 2
53605351 AND cc .total_columns > 3
5352+ AND ip .total_rows > 0
53615353 AND ip .total_reserved_MB >= CASE WHEN (@GetAllDatabases = 1 OR @Mode = 0 ) THEN @ThresholdMB ELSE ip .total_reserved_MB END
53625354 ORDER BY i .db_schema_object_name DESC OPTION ( RECOMPILE );
53635355
@@ -6018,6 +6010,14 @@ BEGIN
60186010 FROM #ForeignKeys fk
60196011 WHERE ([delete_referential_action_desc] <> N ' NO_ACTION'
60206012 OR [update_referential_action_desc] <> N ' NO_ACTION' )
6013+ AND EXISTS (
6014+ SELECT 1 / 0
6015+ FROM #IndexSanity i
6016+ WHERE i .object_id = fk .parent_object_id
6017+ AND i .database_id = fk .database_id
6018+ AND i .schema_name = fk .schema_name
6019+ AND i .user_updates > 0
6020+ )
60216021 OPTION ( RECOMPILE );
60226022
60236023 RAISERROR (N ' check_id 72: Unindexed foreign keys.' , 0 ,1 ) WITH NOWAIT ;
@@ -6041,6 +6041,14 @@ BEGIN
60416041 (SELECT TOP 1 more_info FROM #IndexSanity i WHERE i .object_id = fk .parent_object_id AND i .database_id = fk .database_id AND i .schema_name = fk .schema_name )
60426042 AS more_info
60436043 FROM #UnindexedForeignKeys AS fk
6044+ WHERE EXISTS (
6045+ SELECT 1 / 0
6046+ FROM #IndexSanity i
6047+ WHERE i .object_id = fk .parent_object_id
6048+ AND i .database_id = fk .database_id
6049+ AND i .schema_name = fk .schema_name
6050+ AND (ISNULL (i .user_seeks , 0 ) + ISNULL (i .user_scans , 0 ) + ISNULL (i .user_lookups , 0 )) > 0
6051+ )
60446052 OPTION ( RECOMPILE );
60456053
60466054
0 commit comments