|
36 | 36 | SET NOCOUNT ON; |
37 | 37 | SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; |
38 | 38 | DECLARE @Version VARCHAR(30); |
39 | | -SET @Version = '5.2'; |
40 | | -SET @VersionDate = '20170406'; |
| 39 | +SET @Version = '5.3'; |
| 40 | +SET @VersionDate = '20170501'; |
41 | 41 | IF @Help = 1 PRINT ' |
42 | 42 | /* |
43 | 43 | sp_BlitzIndex from http://FirstResponderKit.org |
@@ -2503,73 +2503,79 @@ 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); |
| 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; |
2508 | 2512 |
|
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, |
| 2513 | + IF NOT (@Mode = 0) |
| 2514 | + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], 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, |
| 2520 | + database_name AS [Database Name], |
2521 | 2521 | N'No indexes use includes' AS finding, 'http://BrentOzar.com/go/IndexFeatures' AS URL, |
2522 | 2522 | N'No indexes use includes' AS details, |
2523 | | - @DatabaseName + N' (Entire database)' AS index_definition, |
| 2523 | + database_name + N' (Entire database)' AS index_definition, |
2524 | 2524 | N'' AS secret_columns, |
2525 | 2525 | 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 ); |
2527 | 2530 |
|
2528 | 2531 | 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) |
2530 | 2533 | INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, |
2531 | 2534 | secret_columns, index_usage_summary, index_size_summary ) |
2532 | 2535 | SELECT 31 AS check_id, |
2533 | 2536 | NULL AS index_sanity_id, |
2534 | 2537 | 150 AS Priority, |
2535 | 2538 | N'Feature-Phobic Indexes' AS findings_group, |
2536 | 2539 | N'Borderline: Includes are used in < 3% of indexes' AS findings, |
2537 | | - @DatabaseName AS [Database Name], |
| 2540 | + database_name AS [Database Name], |
2538 | 2541 | 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, |
2540 | 2543 | N'Entire database' AS index_definition, |
2541 | 2544 | N'' AS secret_columns, |
2542 | 2545 | 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 ); |
2544 | 2550 |
|
2545 | 2551 | 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 ); |
2557 | 2552 |
|
2558 | | - IF @count_filtered_indexes = 0 AND @count_indexed_views=0 AND NOT (@GetAllDatabases = 1 OR @Mode = 0) |
| 2553 | + IF NOT (@Mode = 0) |
2559 | 2554 | INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, |
2560 | 2555 | secret_columns, index_usage_summary, index_size_summary ) |
2561 | | - SELECT 32 AS check_id, |
| 2556 | + SELECT DISTINCT |
| 2557 | + 32 AS check_id, |
2562 | 2558 | NULL AS index_sanity_id, |
2563 | 2559 | 250 AS Priority, |
2564 | 2560 | N'Feature-Phobic Indexes' AS findings_group, |
2565 | 2561 | N'Borderline: No filtered indexes or indexed views exist' AS finding, |
2566 | | - @DatabaseName AS [Database Name], |
| 2562 | + i.database_name AS [Database Name], |
2567 | 2563 | N'http://BrentOzar.com/go/IndexFeatures' AS URL, |
2568 | 2564 | 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, |
2570 | 2566 | N'' AS secret_columns, |
2571 | 2567 | 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 ); |
2573 | 2579 | END; |
2574 | 2580 |
|
2575 | 2581 | RAISERROR(N'check_id 33: Potential filtered indexes based on column names.', 0,1) WITH NOWAIT; |
|
0 commit comments