Rebuilding multiple indexes
When you process both node ID indexes and XML indexes together, they are processed sequentially. First the node ID index is processed and then the XML index.
Building indexes in parallel
Parallel index build reduces the elapsed time for a REBUILD INDEX job by sorting the index keys and rebuilding multiple indexes or index partitions in parallel, rather than sequentially. Optimally, a pair of subtasks processes each index; one subtask sorts extracted keys, while the other subtask builds the index. REBUILD INDEX begins building each index as soon as the corresponding sort generates its first sorted record. If you specify STATISTICS, a third subtask collects the sorted keys and updates the catalog table in parallel.
The subtasks that are used for the parallel REBUILD INDEX processing use Db2 connections. If you receive message DSNU397I that indicates that the REBUILD INDEX utility is constrained, increase the number of concurrent connections by using the MAX BATCH CONNECT parameter on panel DSNTIPE.
The greatest elapsed processing-time improvements result from parallel rebuilding for:
- Multiple indexes on a table space
- A partitioning index or a data-partitioned secondary index on all partitions of a partitioned table space
- A nonpartitioned secondary index on a partitioned table space
The following figure shows the flow of a REBUILD INDEX job with a parallel index build. The same flow applies whether you rebuild a data-partitioned secondary index or a partitioning index. Db2 starts multiple subtasks to unload the entire partitioned table space. Subtasks then sort index keys and build the partitioning index in parallel. If you specify STATISTICS, additional subtasks collect the sorted keys and update the catalog table in parallel, eliminating the need for a second scan of the index by a separate RUNSTATS job.

The following figure shows the flow of a REBUILD INDEX job with a parallel index build. Db2 starts multiple subtasks to unload all partitions of a partitioned table space and to sort index keys in parallel. The keys are then merged and passed to the build subtask, which builds the nonpartitioned secondary index. If you specify STATISTICS, a separate subtask collects the sorted keys and updates the catalog table.

- When parallel index build is used:
- REBUILD INDEX always sorts the index keys and builds them in parallel for partitioned table spaces unless constrained by available memory, sort work files, or UTPRINnn file allocations.
- Sort work data sets for parallel index build:
- You can either allow the utility to dynamically allocate the data sets that SORT needs, or
provide the necessary data sets yourself. Select one of the following methods to allocate sort work
data sets and message data sets:
- Method 1:
- REBUILD INDEX determines the optimal number of sort work data sets and message data sets.
- Specify the SORTDEVT keyword in the utility statement.
- Allow dynamic allocation of sort work data sets by not supplying SORTWKnn DD statements in the REBUILD INDEX utility JCL.
- Allocate UTPRINT to SYSOUT.
- Method 2:
- You control allocation of sort work data sets, and REBUILD INDEX allocates message data sets.
Provide DD statements with DD names in the form SWnnWKmm. The first of these DD statements must be SW01WK01.
- Allocate UTPRINT to SYSOUT.
- Method 3:
- You have the most control over rebuild processing; you must specify both sort work data sets and
message data sets.
Provide DD statements with DD names in the form SWnnWKmm. The first of these DD statements must be SW01WK01.
- Provide DD statements with DD names in the form UTPRINnn.
Data sets that are used
If you select Method 2 or 3, define the necessary data sets by using the following information.
Each sort subtask must have its own group of sort work data sets and its own print message data set. In addition, you need to allocate the merge message data set when you build a single nonpartitioned secondary index on a partitioned table space.
Possible reasons to allocate data sets in the utility job JCL rather than using dynamic allocation are to:
- Control the size and placement of the data sets
- Minimize device contention
- Optimally use free disk space
- Limit the number of utility subtasks that are used to build indexes
- SW01WK01
- Is the first sort work data set that is used by the subtask that builds the first index.
- SW01WK02
- Is the second sort work data set that is used by the subtask that builds the first index.
- SW02WK01
- Is the first sort work data set that is used by the subtask that builds the second index.
- SW02WK02
- Is the second sort work data set that is used by the subtask that builds the second index.
The DD names UTPRINnn define the sort work message data sets that are used by the utility subtask pairs. nn identifies the subtask pair.
Every time you invoke REBUILD INDEX, new UTPRINnn data sets are dynamically allocated. REBUILD INDEX does not reuse UTPRINnn data sets from previous job steps. This behavior might cause the available JES2 job queue elements to be consumed more quickly than expected.
If you allocate the UTPRINT DD statement to SYSOUT in the job statement, the sort message data sets and the merge message data set, if required, are dynamically allocated. If you want the sort message data sets, merge message data sets, or both, allocated to a disk or tape data set rather than to SYSOUT, you must supply the UTPRINnn or the UTMERG01 DD statements (or both) in the utility JCL. If you do not allocate the UTPRINT DD statement to SYSOUT, and you do not supply a UTMERG01 DD statement in the job statement, partitions are not unloaded in parallel.
Determining the number of sort subtasks
The maximum number of utility subtasks that are started for parallel index build equals:
- For a simple table space, segmented (non-UTS) table space, or simple partition of a partitioned table space, the number of indexes that are to be built
- For a single index that is being built on a partitioned table space, the number of partitions that are to be unloaded
REBUILD INDEX determines the number of subtasks according to the following guidelines:
- The number of subtasks equals the number of allocated sort work data set groups.
- The number of subtasks equals the number of allocated message data sets.
- If you allocate both sort work data sets and message data set groups, the number of subtasks equals the smallest number of allocated data sets.
Allocation of sort subtasks
REBUILD INDEX attempts to assign one sort subtask for each index that is to be built. If REBUILD INDEX cannot start enough subtasks to build one index per subtask, it allocates any excess indexes across the pairs (in the order that the indexes were created), so that one or more subtasks might build more than one index.
Estimating the size of the sort work data sets for inline statistics
If you run REBUILD INDEX with the STATISTICS option and distribution statistics are gathered on a data-partitioned secondary index, additional data sets are needed for the statistics aggregate sort. If you choose to provide the data sets, you need to know the size and number of keys that are present in all of the indexes or index partitions that are being processed by the subtask in order to calculate each sort work file size. When you determine which indexes or index partitions are assigned to which subtask pairs, use the following formula to calculate the required space.
2 ×(maximum record length × numcols × (count + 2) × number of indexes)
The variables in the preceding formula have the following values:
- maximum record length
- Maximum record length of the SYSCOLDISTSTATS record that is processed when collecting frequency statistics (You can obtain this value from the RECLENGTH column in SYSTABLES.)
- numcols
- Number of key columns to concatenate when you collect frequent values from the specified index.
- count
- Number of frequent values that Db2 is to collect.
Overriding dynamic sort work data set allocation
Db2 estimates how many records are to be sorted. This information is used for dynamic allocation of sort work space. Sort work space is allocated by Db2 or by the sort program that is used.
If the table space contains rows with VARCHAR columns, Db2 might not be able to accurately estimate the number of records. If the estimated number of records is too high, if the requested sort work space is not available, or if the estimated number of records is too low, which causes the sort to overflow, the utility might fail and cause an abend.
- For a table space that is partitioned (non-universal), run RUNSTATS UPDATE ALL before REORG.
- For any other type of table space, run RUNSTATS UPDATE SPACE before REORG.
You can override the dynamic allocation of sort work space in one of the following ways:
- Allocate the sort work data sets with SORTWKnn DD statements in your JCL.
- If the number of keys for the affected index in column TOTALENTRIES of table SYSIBM.SYSINDEXSPACESTATS is not available or is significantly incorrect, you can update the value to a more appropriate value using an SQL UPDATE statement. When REBUILD INDEX on the affected index completes, TOTALENTRIES is set to the number of keys for the affected index.
- If the number of rows in the associated table space in column TOTALROWS of table SYSIBM.SYSTABLESPACESTATS is not available or is significantly incorrect, you can update the value to a more appropriate value using an SQL UPDATE statement. The next time that REORG is run, TOTALROWS is set to the number of rows in the associated table space.