Skip to content

Commit 55ad36e

Browse files
add schema filtering
allow users to filter to only tables in a specific schema
1 parent 7ebc346 commit 55ad36e

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

sp_IndexCleanup/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ The procedure requires SQL Server 2012 (11.0) or later due to the use of FORMAT
2323
| Parameter Name | Data Type | Default Value | Description |
2424
|----------------|-----------|---------------|-------------|
2525
| @database_name | sysname | NULL | The name of the database you wish to analyze |
26-
| @schema_name | sysname | NULL | The schema name to filter indexes by |
26+
| @schema_name | sysname | NULL | The schema name to filter indexes by - limits analysis to tables in the specified schema |
2727
| @table_name | sysname | NULL | The table name to filter indexes by |
2828
| @min_reads | bigint | 0 | Minimum number of reads for an index to be considered used |
2929
| @min_writes | bigint | 0 | Minimum number of writes for an index to be considered used |
@@ -56,6 +56,11 @@ EXECUTE dbo.sp_IndexCleanup
5656
@database_name = 'YourDatabase',
5757
@dedupe_only = 1;
5858

59+
-- Analyze tables in a specific schema only
60+
EXECUTE dbo.sp_IndexCleanup
61+
@database_name = 'YourDatabase',
62+
@schema_name = 'YourSchema';
63+
5964
-- Filter indexes by minimum usage thresholds
6065
EXECUTE dbo.sp_IndexCleanup
6166
@database_name = 'YourDatabase',

sp_IndexCleanup/sp_IndexCleanup.sql

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ BEGIN TRY
104104
CASE
105105
ap.name
106106
WHEN N'@database_name' THEN 'the name of the database you wish to analyze'
107-
WHEN N'@schema_name' THEN 'the schema name to filter indexes by'
107+
WHEN N'@schema_name' THEN 'the schema name to filter indexes by - limits analysis to tables in the specified schema'
108108
WHEN N'@table_name' THEN 'the table name to filter indexes by'
109109
WHEN N'@min_reads' THEN 'minimum number of reads for an index to be considered used'
110110
WHEN N'@min_writes' THEN 'minimum number of writes for an index to be considered used'
@@ -1137,6 +1137,17 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11371137
SELECT @sql += N'
11381138
AND t.object_id = @object_id';
11391139
END;
1140+
1141+
IF @schema_name IS NOT NULL AND @object_id IS NULL
1142+
BEGIN
1143+
IF @debug = 1
1144+
BEGIN
1145+
RAISERROR('adding schema_name filter', 0, 0) WITH NOWAIT;
1146+
END;
1147+
1148+
SELECT @sql += N'
1149+
AND s.name = @schema_name';
1150+
END;
11401151

11411152
SET @sql += N'
11421153
AND EXISTS
@@ -1208,13 +1219,15 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12081219
@min_writes bigint,
12091220
@min_size_gb decimal(10,2),
12101221
@min_rows bigint,
1211-
@object_id integer',
1222+
@object_id integer,
1223+
@schema_name sysname',
12121224
@current_database_id,
12131225
@min_reads,
12141226
@min_writes,
12151227
@min_size_gb,
12161228
@min_rows,
1217-
@object_id;
1229+
@object_id,
1230+
@schema_name;
12181231

12191232
SET @rc = ROWCOUNT_BIG();
12201233

0 commit comments

Comments
 (0)