@@ -2503,73 +2503,89 @@ BEGIN;
2503
2503
https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/issues/825
2504
2504
*/
2505
2505
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 )
2515
2514
INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, URL , details, index_definition,
2516
2515
secret_columns, index_usage_summary, index_size_summary )
2517
- SELECT 30 AS check_id,
2516
+ SELECT 30 AS check_id,
2518
2517
NULL AS index_sanity_id,
2519
2518
250 AS Priority,
2520
2519
N ' Feature-Phobic Indexes' AS findings_group,
2521
2520
N ' No indexes use includes' AS finding, ' http://BrentOzar.com/go/IndexFeatures' AS URL ,
2522
2521
N ' No indexes use includes' AS details,
2523
- @DatabaseName + N ' (Entire database)' AS index_definition,
2522
+ database_name + N ' (Entire database)' AS index_definition,
2524
2523
N ' ' AS secret_columns,
2525
2524
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 );
2527
2529
2528
2530
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 )
2530
2532
INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL , details, index_definition,
2531
2533
secret_columns, index_usage_summary, index_size_summary )
2532
2534
SELECT 31 AS check_id,
2533
2535
NULL AS index_sanity_id,
2534
2536
150 AS Priority,
2535
2537
N ' Feature-Phobic Indexes' AS findings_group,
2536
2538
N ' Borderline: Includes are used in < 3% of indexes' AS findings,
2537
- @DatabaseName AS [Database Name],
2539
+ database_name AS [Database Name],
2538
2540
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,
2540
2542
N ' Entire database' AS index_definition,
2541
2543
N ' ' AS secret_columns,
2542
2544
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 );
2544
2549
2545
2550
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
2551
2549
- SELECT @count_filtered_indexes= COUNT (* )
2552
+ SELECT database_name ,
2553
+ COUNT (* ) AS count_filtered_indexes
2554
+ INTO #filtered_index_count
2550
2555
FROM #IndexSanity
2551
- WHERE filter_definition <> ' ' OPTION ( RECOMPILE );
2556
+ WHERE filter_definition <> ' '
2557
+ GROUP BY database_name
2558
+ OPTION ( RECOMPILE );
2552
2559
2553
- SELECT @count_indexed_views= COUNT (* )
2560
+ SELECT i .database_name ,
2561
+ COUNT (* ) AS count_indexed_views
2562
+ INTO #indexed_view_count
2554
2563
FROM #IndexSanity AS i
2555
2564
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 );
2557
2568
2558
- IF @count_filtered_indexes = 0 AND @count_indexed_views = 0 AND NOT (@GetAllDatabases = 1 OR @Mode = 0 )
2569
+ IF NOT (@Mode = 0 )
2559
2570
INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL , details, index_definition,
2560
2571
secret_columns, index_usage_summary, index_size_summary )
2561
- SELECT 32 AS check_id,
2572
+ SELECT 32 AS check_id,
2562
2573
NULL AS index_sanity_id,
2563
2574
250 AS Priority,
2564
2575
N ' Feature-Phobic Indexes' AS findings_group,
2565
2576
N ' Borderline: No filtered indexes or indexed views exist' AS finding,
2566
- @DatabaseName AS [Database Name],
2577
+ fic . database_name AS [Database Name],
2567
2578
N ' http://BrentOzar.com/go/IndexFeatures' AS URL ,
2568
2579
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,
2570
2581
N ' ' AS secret_columns,
2571
2582
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 );
2573
2589
END ;
2574
2590
2575
2591
RAISERROR (N ' check_id 33: Potential filtered indexes based on column names.' , 0 ,1 ) WITH NOWAIT ;
0 commit comments