Skip to content

Commit d654cbd

Browse files
committed
Skip inactive indexes
Adds a parameter to skip indexes with no reads or writes that defaults to skipping them, unless you: * Override it * Are in a mode that's not 0 or 4 * TableName is not null
1 parent 3949dcc commit d654cbd

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

sp_BlitzIndex.sql

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ ALTER PROCEDURE dbo.sp_BlitzIndex
3131
@OutputDatabaseName NVARCHAR(256) = NULL ,
3232
@OutputSchemaName NVARCHAR(256) = NULL ,
3333
@OutputTableName NVARCHAR(256) = NULL ,
34+
@IncludeInactiveIndexes BIT = 0 /* Will skip indexes with no reads or writes */,
3435
@Help TINYINT = 0,
3536
@Debug BIT = 0,
3637
@Version VARCHAR(30) = NULL OUTPUT,
@@ -1208,10 +1209,16 @@ BEGIN TRY
12081209
CASE WHEN si.filter_definition IS NOT NULL THEN si.filter_definition
12091210
ELSE N''''
12101211
END AS filter_definition' ELSE N''''' AS filter_definition' END + N'
1211-
, ISNULL(us.user_seeks, 0), ISNULL(us.user_scans, 0),
1212-
ISNULL(us.user_lookups, 0), ISNULL(us.user_updates, 0), us.last_user_seek, us.last_user_scan,
1213-
us.last_user_lookup, us.last_user_update,
1214-
so.create_date, so.modify_date
1212+
, ISNULL(us.user_seeks, 0),
1213+
ISNULL(us.user_scans, 0),
1214+
ISNULL(us.user_lookups, 0),
1215+
ISNULL(us.user_updates, 0),
1216+
us.last_user_seek,
1217+
us.last_user_scan,
1218+
us.last_user_lookup,
1219+
us.last_user_update,
1220+
so.create_date,
1221+
so.modify_date
12151222
FROM ' + QUOTENAME(@DatabaseName) + N'.sys.indexes AS si WITH (NOLOCK)
12161223
JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.objects AS so WITH (NOLOCK) ON si.object_id = so.object_id
12171224
AND so.is_ms_shipped = 0 /*Exclude objects shipped by Microsoft*/
@@ -1222,8 +1229,14 @@ BEGIN TRY
12221229
AND us.database_id = ' + CAST(@DatabaseID AS NVARCHAR(10)) + N'
12231230
WHERE si.[type] IN ( 0, 1, 2, 3, 4, 5, 6 )
12241231
/* Heaps, clustered, nonclustered, XML, spatial, Cluster Columnstore, NC Columnstore */ ' +
1225-
CASE WHEN @TableName IS NOT NULL THEN N' and so.name=' + QUOTENAME(@TableName,N'''') + N' ' ELSE N'' END +
1226-
N'OPTION ( RECOMPILE );
1232+
CASE WHEN @TableName IS NOT NULL THEN N' and so.name=' + QUOTENAME(@TableName,N'''') + N' ' ELSE N'' END +
1233+
CASE WHEN ( @IncludeInactiveIndexes = 0
1234+
OR @Mode NOT IN (0, 4)
1235+
OR @TableName IS NOT NULL )
1236+
THEN N'AND ( us.user_seeks + us.user_scans + us.user_lookups + us.user_updates ) > 0'
1237+
ELSE N''
1238+
END
1239+
+ N'OPTION ( RECOMPILE );
12271240
';
12281241
IF @dsql IS NULL
12291242
RAISERROR('@dsql is null',16,1);

0 commit comments

Comments
 (0)