Professional Documents
Culture Documents
Indexes provide a fast and efficient method of retrieving selected data from a table. By pointing to the
blocks that contain the selected data, the entire table does not have to read in order to extract the
required information. Most indexes in Oracle databases are built using the B-tree data structure. While Btree indexes in Oracle are always balanced, they do not clean up any free space after leaf entries have
been moved due to an UPDATE statement or removed due to a DELETE statement. Too many empty
leaf entries in an index can cause the index to be too high, which can cause poor performance.
65
1 row selected.
Page 1 of 4
There are two rules of thumb to help determine if the index needs to be rebuilt. If it is determined that the
index needs to be rebuilt, this can easily be accomplished by the ALTER INDEX REBUILD command.
Although not necessarily recommended, this command could be executed during normal operating hours.
You can rebuild the index ONLINE, but this can consume extra resources. Rebuilding the index uses the
existing index as a basis. The biggest benefit is that the leaf entries do not have to be resorted. It is
already sorted in the existing index. The alternative is to drop and re-create the index. Creating an index
uses the base table as its data source that needs to put a lock on the table. The index is also unavailable
during creation. The data must already be sorted, which is the biggest contributor to the index creation
time.
First rule of thumb: If the index has height greater than four, rebuild the index. For most indexes, the
height of the index will be quite low, i.e. one or two. There have been examples of an index on a 3 millionrow table that had height three. An index with height greater than four may need to be rebuilt as this might
indicate a tree structure with too many deleted leaf entries. This can lead to unnecessary database block
reads of the index. It is helpful to know the data structure for the table and index. Most times, the index
height should be two or less, but there are exceptions.
Second rule of thumb: The deleted leaf rows should be less than 20% of the total number of leaf
rows. An excessive number of deleted leaf rows indicates that a high number of deletes or updates have
occurred to the index column(s). The index should be rebuilt to better balance the index entries in the
tree. The INDEX_STATS table can be queried to determine if there are excessive deleted leaf rows in
relation to the total number of leaf rows.
For example:
SQLWKS> ANALYZE INDEX item_basket_pk VALIDATE STRUCTURE;
Statement processed.
SQLWKS> SELECT name,height,lf_rows,del_lf_rows,(del_lf_rows/lf_rows)*100
ratio FROM INDEX_STATS;
NAME
HEIGHT
LF_ROWS
DEL_LF_ROW
RATIO
------------------ ---------- ---------- ----------- -----------ITEM_BASKET_PK
1
235
74
31.4893617
as
In this example, the ratio of deleted leaf rows to total leaf rows is clearly above 20%. This is a good
candidate for rebuilding. Let's rebuild the index and examine the results.
SQLWKS> ALTER INDEX item_basket_pk REBUILD;
Statement processed.
SQLWKS> ANALYZE INDEX item_basket_pk VALIDATE STRUCTURE;
Statement processed.
SQLWKS> SELECT name,height,lf_rows,del_lf_rows,(del_lf_rows/lf_rows)*100
ratio FROM INDEX_STATS;
NAME
HEIGHT
LF_ROWS
DEL_LF_ROW
------------------ ---------- ---------- -----------ITEM_BASKET_PK
1
161
0
1 row selected.
as
RATIO
-----0
The index is rebuilt and validated once again. Examining the INDEX_STATS table shows that the 74
deleted leaf rows were dropped from the index. Notice that the total number of leaf rows went from 235 to
161, which is a difference of 74 leaf rows. This index should provide better performance for the
application.
Page 2 of 4
HEIGHT: Height of the index, which begins at 1 for root only index.
BLOCKS: Number of blocks allocated to the index.
LF_ROWS: Number of leaf row entries (includes deleted row entries).
DEL_LF_ROWS: Number of deleted leaf row entries not yet cleaned out.
USED_SPACE: Total space used within the index (includes deleted entries).
PCT_USED: Percentage of space used within the index (includes deleted entries).
This is derived by the following formula: (USED_SPACE/BTREE_SPACE)*100.
BTREE_SPACE: Total size of the index (includes deleted entries).
You can estimate the non-deleted rows in an index by subtracting the DEL_LF_ROWS value from the
LF_ROWS value. You can estimate the percentage of space used by the non-deleted rows of an indexed
by using the following formula: ((USED_SPACE - DEL_LF_ROWS_LEN)/BTREE_SPACE) * 100
Page 3 of 4
Index_Stats Table
Column Name
Significance
OPT_CMPR_PCTSAVE
HEIGHT
BLOCKS
NAME
PARTITION_NAME
LF_ROWS
LF_BLKS
LF_ROWS_LEN
LF_BLK_LEN
BR_ROWS
BR_BLKS
BR_ROWS_LEN
BR_BLK_LEN
DEL_LF_ROWS
DEL_LF_ROWS_LEN
DISTINCT_KEYS
MOST_REPEATED_KEY
BTREE_SPACE
USED_SPACE
PCT_USED
ROWS_PER_KEY
BLKS_GETS_PER_ACCESS
PRE_ROWS
PRE_ROWS_LEN
OPT_CMPR_COUNT
Page 4 of 4