RelationalDBDesign RelationalDBDesign

Oracle Indexes   «Prev  Next»
Lesson 7 Rebuilding an index
Objective Modify the way an index is stored.

Rebuilding Oracle Index

Although an index is an optimized way to access data, the index itself can become inefficient as values are added and deleted to the index. As users change the contents, the index can become fragmented, with holes existing where values were deleted.
When there is a lot of wasted space in the index, Oracle will incur added I/O costs as it retrieves index nodes and leaf blocks that contain wasted space. You may end up with an extra level of index nodes, which means that every index access will take an extra I/O in order to get to the leaf blocks. You also may end up with more leaf blocks than necessary, which would require additional I/O in some situations.

Cleaning up empty space

The main way to remove empty space in an index is to rebuild it. You rebuild an index with the ALTER INDEX command:

ALTER INDEX bid_auction_idx REBUILD;

The REBUILD command will completely rebuild the index, filling each of the index nodes and leaf blocks optimally. Your index should work most efficiently after a REBUILD.
You can also coalesce an index, using the keyword COALESCE with the ALTER INDEX command. Coalescing an index only operates on the leaf blocks in the index, by compressing all the empty space and storing it in its own leaf blocks. If you had four leaf blocks that were 50% full, a COALESCE would give you two leaf blocks that were 100% empty.
An index can also become unbalanced, because the values being added to the index are greater than the previous values in the index. You can address this situation with a reverse key index.

Reuse of Index Space

The database can reuse space within an index block. For example, if you insert a value into a column and delete it, and if an index exists on this column, then the database can reuse the index slot when a row requires it.
The database can reuse an index block itself. Unlike a table block, an index block only becomes free when it is empty. The database places the empty block on the free list of the index structure and makes it eligible for reuse. However, Oracle Database does not automatically compact the index: an ALTER INDEX REBUILD or COALESCE statement is required.
Figure 5-1 represents an index of the employees.department_id column before the index is coalesced. The first three leaf blocks are only partially full, as indicated by the gray fill lines.
Figure5-1 Index Before Coalescing
Figure 5-2 shows the index in Figure 5-1 after the index has been coalesced. The first two leaf blocks are now full, as indicated by the gray fill lines, and the third leaf block has been freed.
Figure5-2 Index After Coalescing

Altering Indexes - Quiz

Click the Quiz link below to answer a few questions about altering indexes.
Altering Indexes - Quiz
The next lesson shows how to get information about indexes from the data dictionary.