Skip to content

Commit c653bfb

Browse files
authored
Merge pull request BrentOzarULTD#3958 from BrentOzarULTD/claude/amazing-swanson-c31f97
3957_sp_BlitzIndex_prefixing
2 parents 96f31f8 + e5aec61 commit c653bfb

1 file changed

Lines changed: 19 additions & 18 deletions

File tree

sp_BlitzIndex.sql

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5197,50 +5197,51 @@ BEGIN
51975197
RAISERROR(N'check_id 21: More Than 5 Percent NC Indexes Are Unused', 0,1) WITH NOWAIT;
51985198
INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition,
51995199
secret_columns, index_usage_summary, index_size_summary )
5200-
SELECT 21 AS check_id,
5201-
MAX(i.index_sanity_id) AS index_sanity_id,
5200+
SELECT 21 AS check_id,
5201+
MAX(i.index_sanity_id) AS index_sanity_id,
52025202
150 AS Priority,
52035203
N'Over-Indexing' AS findings_group,
52045204
N'More Than 5 Percent NC Indexes Are Unused' AS finding,
5205-
[database_name] AS [Database Name],
5205+
i.[database_name] AS [Database Name],
52065206
N'https://www.brentozar.com/go/IndexHoarder' AS URL,
52075207
CAST (MAX(perc.percent_NC_indexes_unused) AS NVARCHAR(30)) + N' percent NC indexes (' + CAST(COUNT(*) AS NVARCHAR(10)) + N') unused. ' +
52085208
N'These take up ' + CAST (MAX(perc.NC_indexes_unused_reserved_MB) AS NVARCHAR(30)) + N'MB of space.' AS details,
52095209
i.database_name + ' (' + CAST (COUNT(*) AS NVARCHAR(30)) + N' indexes)' AS index_definition,
5210-
'' AS secret_columns,
5211-
CAST(SUM(total_reads) AS NVARCHAR(256)) + N' reads (ALL); '
5212-
+ CAST(SUM([user_updates]) AS NVARCHAR(256)) + N' writes (ALL)' AS index_usage_summary,
5213-
5214-
REPLACE(CONVERT(NVARCHAR(30),CAST(MAX([total_rows]) AS MONEY), 1), '.00', '') + N' rows (MAX)'
5215-
+ CASE WHEN SUM(total_reserved_MB) > 1024 THEN
5216-
N'; ' + CAST(CAST(SUM(total_reserved_MB)/1024. AS NUMERIC(29,1)) AS NVARCHAR(30)) + 'GB (ALL)'
5217-
WHEN SUM(total_reserved_MB) > 0 THEN
5218-
N'; ' + CAST(CAST(SUM(total_reserved_MB) AS NUMERIC(29,1)) AS NVARCHAR(30)) + 'MB (ALL)'
5210+
'' AS secret_columns,
5211+
CAST(SUM(i.total_reads) AS NVARCHAR(256)) + N' reads (ALL); '
5212+
+ CAST(SUM(i.[user_updates]) AS NVARCHAR(256)) + N' writes (ALL)' AS index_usage_summary,
5213+
5214+
REPLACE(CONVERT(NVARCHAR(30),CAST(MAX(sz.[total_rows]) AS MONEY), 1), '.00', '') + N' rows (MAX)'
5215+
+ CASE WHEN SUM(sz.total_reserved_MB) > 1024 THEN
5216+
N'; ' + CAST(CAST(SUM(sz.total_reserved_MB)/1024. AS NUMERIC(29,1)) AS NVARCHAR(30)) + 'GB (ALL)'
5217+
WHEN SUM(sz.total_reserved_MB) > 0 THEN
5218+
N'; ' + CAST(CAST(SUM(sz.total_reserved_MB) AS NUMERIC(29,1)) AS NVARCHAR(30)) + 'MB (ALL)'
52195219
ELSE ''
52205220
END AS index_size_summary
52215221
FROM #IndexSanity i
52225222
JOIN #IndexSanitySize sz ON i.index_sanity_id = sz.index_sanity_id
52235223
JOIN (
52245224
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
5225+
CAST((100.00 * SUM(CASE WHEN i.total_reads = 0 THEN 1 ELSE 0 END)) / COUNT(*) AS NUMERIC(29,1)) AS percent_NC_indexes_unused,
5226+
CAST(SUM(CASE WHEN i.total_reads = 0 THEN sz.total_reserved_MB ELSE 0 END) AS NUMERIC(29,1)) AS NC_indexes_unused_reserved_MB
52275227
FROM #IndexSanity i
52285228
JOIN #IndexSanitySize sz ON i.index_sanity_id = sz.index_sanity_id
5229-
WHERE index_id NOT IN ( 0, 1 )
5229+
WHERE i.index_id NOT IN ( 0, 1 )
52305230
AND i.is_unique = 0
52315231
/*Skipping tables created in the last week, or modified in past 2 days*/
52325232
AND i.create_date < DATEADD(dd,-7,GETDATE())
52335233
AND i.modify_date < DATEADD(dd,-2,GETDATE())
52345234
GROUP BY i.database_name
52355235
) AS perc ON i.database_name = perc.database_name
5236-
WHERE index_id NOT IN ( 0, 1 )
5236+
WHERE i.index_id NOT IN ( 0, 1 )
52375237
AND i.is_unique = 0
5238-
AND total_reads = 0
5238+
AND i.total_reads = 0
52395239
/*Skipping tables created in the last week, or modified in past 2 days*/
52405240
AND i.create_date < DATEADD(dd,-7,GETDATE())
52415241
AND i.modify_date < DATEADD(dd,-2,GETDATE())
52425242
AND perc.percent_NC_indexes_unused >= 5
5243-
GROUP BY i.database_name
5243+
AND sz.total_reserved_MB >= CASE WHEN (@GetAllDatabases = 1 OR @Mode = 0) THEN @ThresholdMB ELSE sz.total_reserved_MB END
5244+
GROUP BY i.database_name
52445245
OPTION ( RECOMPILE );
52455246

52465247
RAISERROR(N'check_id 23: Indexes with 7 or more columns. (Borderline)', 0,1) WITH NOWAIT;

0 commit comments

Comments
 (0)