When an index is in REBUILD-pending (RBDP), was created with DEFER YES, or you need to reconstruct keys after recovering a table, you do not “fix pages” in the index—you rebuild it from the table. The IBM REBUILD INDEX utility is that job in DB2 for z/OS. This page covers naming indexes versus INDEX(ALL), partitioned and nonpartitioned rebuilds, SHRLEVEL REFERENCE and CHANGE, SCOPE PENDING, REUSE, sorts, and inline statistics.
It reads the referenced table, unloads keys, sorts them, and writes a new index. Unless you specify REUSE, Db2-managed data sets are deleted and redefined (useful with DFSMS encryption). You can create a FlashCopy image copy of the rebuilt indexes and collect STATISTICS inline so a separate RUNSTATS is unnecessary.
| Phase | What happens |
|---|---|
| UTILINIT | Setup |
| UNLOAD | Unload index keys from the table |
| SORT | Sort keys |
| BUILD | Build the index |
| SORTBLD | Parallel sort-and-build when multiple indexes are built together |
| LOG | Iterative log apply for SHRLEVEL CHANGE only |
| UTILTERM | Cleanup |
1234567891011REBUILD INDEX(PAYROLL.IXEMPNO) SORTDEVT SYSDA STATISTICS REPORT YES UPDATE ALL; REBUILD INDEX(ALL) TABLESPACE PAYDB.PAYTS SCOPE PENDING SORTDEVT SYSDA; REBUILD INDEX(PAYROLL.IXNPSI) PART 4 SHRLEVEL REFERENCE SORTDEVT SYSDA;
A partitioning index or data-partitioned secondary index (DPSI) has a physical data set per partition. REBUILD PART n rebuilds that physical piece.
A nonpartitioned secondary index (NPSI) is one data set covering all partitions. PART n rebuilds the logical partition (keys for that table partition) except when:
In those cases rebuild the entire NPSI. Prefer one job that lists multiple indexes on the same table space so Db2 can build them in parallel (SORTBLD), rather than parallel jobs fighting over the same table.
| SHRLEVEL | Access during REBUILD |
|---|---|
| REFERENCE | Table/partition readable, not writable. The index being built cannot be read or written. |
| CHANGE | Table remains read-write. Index sits in RBDP (dynamic SQL can avoid it). Invalid for XML indexes, spatial indexes, and NOT LOGGED tables. |
IBM’s restriction: use SHRLEVEL CHANGE to fix a broken or restricted index, to build after DEFER, or to (un)encrypt data sets—not to move volumes (use REORG INDEX). CHANGE on a unique index will not allow INSERT, DELETE, or updates that affect that unique index. Additional CHANGE options:
CREATE INDEX DEFER YES leaves the index empty until REBUILD. Dynamic SQL will not choose the index until it is built. After RECOVER TABLESPACE, dependent indexes are often RBDP; REBUILD INDEX(ALL) SCOPE PENDING is the usual cleanup.
An index is the library card catalog. If the catalog burns, you do not photocopy ash—you walk every shelf (the table) and write a new catalog (REBUILD). SHRLEVEL REFERENCE means visitors may read books but nobody reshelves until you finish. SHRLEVEL CHANGE means people still reshelve while you copy the shelves onto a scratch pad and catch up from the diary (the log)—but a unique catalog cannot promise “no duplicate cards” until it is done, so new unique cards are refused. REUSE is using the same catalog drawers; without REUSE you get brand-new drawers (better after a smashed cabinet).
1. Where does REBUILD INDEX get the keys it writes?
2. What does SCOPE PENDING rebuild?
3. Why is SHRLEVEL CHANGE a poor choice for a unique index under concurrent DML?
4. When must you rebuild an entire NPSI instead of PART n?
5. What does REUSE do?