The IN_DB_UTIL_SP_INDEX_MAINTENANCE stored procedure can be used to perform basic index maintenance including reorganizing and rebuilding indexes to reduce fragmentation or to implement various index option changes to specified tables or indexes.
Note that index maintenance should only be conducted after hours or on the weekends when application usage is very minimal.
Note that in many cases database performance can be improved by simply ensuring statistics are updated frequently using large sample sizes, such as FULLSCAN, rather than rebuilding indexes.
Parameter | Description | |
---|---|---|
MAX_MINUTES | Type: | INPUT |
Datatype: | INTEGER | |
Default Value: | 0 | |
Description |
Specifies a time limit to determine whether to start processing the next index. Each index operation will run through completion but a new index operation will not start if time has expired. |
|
Options |
0 = No Time Limit Any number of minutes greater than 0 |
|
TABLE_NAME | Type: | INPUT |
Datatype: | SYSNAME | |
Default Value: | NULL | |
Description | Specifies the name of the table for which to perform index maintenance on. | |
Options |
Any table in the inuser schema. |
|
INDEX_NAME | Type: | INPUT |
Datatype: | SYSNAME | |
Default Value: | NULL | |
Description | Specifies the name of the index for which to perform index maintenance on. | |
Options | Any index in the inuser schema. | |
FRAGPCT_REORG | Type: | INPUT |
Datatype: | FLOAT | |
Default Value: | 5 | |
Description | The threshold of fragmentation percentage to trigger a reorg of an index. | |
Options |
Between -1 and 100 -1 = Disables the reorganize option |
|
FRAGPCT_REBUILD | Type: | INPUT |
Datatype: | FLOAT | |
Default Value: | 30 | |
Description | The threshold of fragmentation percentage to trigger a rebuild of an index. | |
Options | Between 0 and 100 | |
SORTINTEMPDB | Type: | INPUT |
Datatype: | NVARCHAR(3) | |
Default Value: | ON | |
Description | Specifies whether to store the sort results in tempdb. | |
Options |
ON OFF |
|
FILLFACTOR | Type: | INPUT |
Datatype: | INTEGER | |
Default Value: | 0 | |
Description | The percentage to fill each index page at the leaf level. | |
Options |
0 = Use current setting Any number between 75-100 percent |
|
PADINDEX | Type: | INPUT |
Datatype: | NVARCHAR(3) | |
Default Value: | OFF | |
Description | The percentage of free space that is specified by FILLFACTOR is applied to the intermediate-level pages of the index. | |
Options |
ON OFF |
|
DATACOMPRESSION | Type: | INPUT |
Datatype: | NVARCHAR(4) | |
Default Value: | NONE | |
Description | Specifies the data compression option for the specified index. | |
Options |
NONE ROW PAGE |
|
NORECOMPUTESTATS T | Type: | INPUT |
Datatype: | NVARCHAR(3) | |
Default Value: | OFF | |
Description | Specifies whether distribution statistics are recomputed. | |
Options | ON OFF |
|
MAXDOP | Type: | INPUT |
Datatype: | INTEGER | |
Default Value: | 0 | |
Description | Overrides the max degree of parallelism configuration option for the duration of the index operation. | |
Options |
0 = Use the number of actual processors or fewer based on the current system workload. 1 = Suppresses parallel plan generation. >1 = Restricts the maximum number of processors used in a parallel index operation to the specified number or fewer based on the current system workload. |
|
MODE | Type: | INPUT |
Datatype: | SYSNAME | |
Default Value: | LIMITED | |
Description | The scan level used to obtain fragmentation statistics for each index using dm_db_index_physical_stats. | |
Options |
DEFAULT LIMITED SAMPLED DETAILED |
|
ONLINE | Type: | INPUT |
Datatype: | NVARCHAR(3) | |
Default Value: | ON | |
Description | Specifies whether underlying tables and associated indexes are available for queries and data modification during the index operation. | |
Options |
ON OFF |
|
ONLINE_WAIT_MINS | Type: | INPUT |
Datatype: | INTEGER | |
Default Value: | 0 | |
Description | Specifies the MAX_DURATION parameter for WAIT_AT_LOW_PRIORITY for online index operations. | |
Options |
ON |
|
ONLINE_WAIT_ABORT | Type: | INPUT |
Datatype: | NVARCHAR(8) | |
Default Value: | NONE | |
Description | Specifies the ABORT_AFTER_WAIT parameter for WAIT_AT_LOW_PRIORITY for online index operations. | |
Options |
NONE SELF BLOCKERS |
|
INCLUDE_DISABLED | Type: | INPUT |
Datatype: | NVARCHAR(3) | |
Default Value: | NO | |
Description | Specifies if disabled indexes should be rebuilt. This will enable the indexes as well. Does not apply to index reorganization. | |
Options |
NO = Ignore disabled indexes and do not rebuild YES = Rebuild and enable any disabled indexes for the specified table |
|
CHECK_ONLY | Type: | INPUT |
Datatype: | NVARCHAR(3) | |
Default Value: | NO | |
Description | Specifies to only build a fragmentation report instead of reorganizing or rebuilding the indexes. | |
Options |
NO = Alter Indexes YES = Report Only |
|
SHOW_SQL | Type: | INPUT |
Datatype: | NVARCHAR(3) | |
Default Value: | YES | |
Description | Specifies whether to display the ALTER INDEX SQL statement that is being executed. | |
Options |
YES = Display the SQL NO = Do not display the SQL |
|
DEBUG | Type: | INPUT |
Datatype: | NVARCHAR(3) | |
Default Value: | NO | |
Description |
Specifies whether to display additional debugging information during execution, including background operations such as session setup and state changes and call stack details. |
|
Options |
NO = Do not display additional details to the Message tab. YES = Display additional details to the Messages tab. |
|
RETURN | Type: | RETURN |
Datatype: | INTEGER | |
Default Value: | 0 | |
Description |
Specifies whether the execution of the procedure was successful or not. 0 = Success 1 = Error |
Examples
The following example is the default and will reorganize any index that is between 5 and 30 percent fragmented and rebuild any index that is greater than 30 percent fragmented. With MAX_MINUTES set to 0 (no time limit) the procedure will continue running until all qualifying indexes have been reorganized or rebuilt.
EXEC IN_INDEX_MAINTENANCE @MAX_MINUTES = 0, @TABLE_NAME = NULL, @INDEX_NAME = NULL, @FRAGPCT_REORG = 5, @FRAGPCT_REBUILD = 30, @SORTINTEMPDB = N'ON', @FILLFACTOR = 0, @PADINDEX = N'OFF', @DATACOMPRESSION = N'NONE', @NORECOMPUTESTATS = N'OFF', @MAXDOP = 0, @MODE = N'LIMITED', @ONLINE = N'ON', @ONLINE_WAIT_MINS = 0, @ONLINE_WAIT_ABORT = N'NONE', @INCLUDE_DISABLED = N'NO', @CHECK_ONLY = N'NO', @SHOW_SQL = N'NO', @DEBUG = N'NO';
The following example is equivalent to the example above.
EXEC IN_INDEX_MAINTENANCE;
The following will force an index rebuild for all indexes on the IN_DOC table and rebuild the indexes using a fillfactor of 95 percent.
EXEC IN_INDEX_MAINTENANCE @MAX_MINUTES = 0, @TABLE_NAME = 'IN_DOC', @FRAGPCT_REORG = -1, @FRAGPCT_REBUILD = 0, @FILLFACTOR = 95;
The following will force an index rebuild for all indexes on the IN_INSTANCE_PROP table and rebuild the indexes using page level compression.
EXEC IN_INDEX_MAINTENANCE @MAX_MINUTES = 0, @TABLE_NAME = 'IN_INSTANCE_PROP', @FRAGPCT_REORG = -1, @FRAGPCT_REBUILD = 0, @DATACOMPRESSION = 'PAGE';
The following will build an index fragmentation report and display the generated SQL to reorganize or rebuild the indexes without executing the SQL to reorganize or rebuild any of indexes
EXEC IN_INDEX_MAINTENANCE @CHECK_ONLY = 'YES', @SHOW_SQL='YES'