|Oracle9i Database Performance Tuning Guide and Reference
Release 2 (9.2)
Part Number A96533-02
This chapter provides an overview of data access methods using indexes and clusters that can enhance or degrade performance.
The chapter contains the following sections:
This section describes the following:
Although cost-based optimization helps avoid the use of nonselective indexes within query execution, the SQL engine must continue to maintain all indexes defined against a table, regardless of whether they are used. Index maintenance can present a significant CPU and I/O resource demand in any write-intensive application. In other words, do not build indexes unless necessary.
To maintain optimal performance, drop indexes that an application is not using. You can find indexes that are not being used by using the
USAGE functionality over a period of time that is representative of your workload. This monitoring feature records whether or not an index has been used. If you find that an index has not been used, then drop it. Be careful to select a representative workload to monitor.
Indexes within an application sometimes have uses that are not immediately apparent from a survey of statement execution plans. An example of this is a foreign key index on a parent table, which prevents share locks from being taken out on a child table.
If you are deciding whether to create new indexes to tune statements, then you can also use the
PLAN statement to determine whether the optimizer will choose to use these indexes when the application is run. If you create new indexes to tune a statement that is currently parsed, then Oracle invalidates the statement. When the statement is next executed, the optimizer automatically chooses a new execution plan that could potentially use the new index. If you create new indexes on a remote database to tune a distributed statement, then the optimizer considers these indexes when the statement is next parsed.
Also keep in mind that the way you tune one statement can affect the optimizer's choice of execution plans for other statements. For example, if you create an index to be used by one statement, then the optimizer can choose to use that index for other statements in the application as well. For this reason, reexamine the application's performance and rerun the SQL trace facility after you have tuned those statements that you initially identified for tuning.
You can use the Oracle Index Tuning Wizard to detect tables with inefficient indexes. The Oracle Index Tuning wizard is an Oracle Enterprise Manager integrated application available with the Oracle Tuning Pack. Similar functionality is available from the Virtual Index Advisor (a feature of SQL Analyze) and Oracle Expert.
A key is a column or expression on which you can build an index. Follow these guidelines for choosing keys to index:
Oracle automatically creates indexes, or uses existing indexes, on the keys and expressions of unique and primary keys that you define with integrity constraints.
Indexing low selectivity columns can be helpful if the data distribution is skewed so that one or two values occur much less often than other values.
UPDATEstatements that modify indexed columns and
DELETEstatements that modify indexed tables take longer than if there were no index. Such SQL statements must modify data in indexes as well as data in tables. They also generate additional undo and redo.
WHEREclauses with functions or operators. A
WHEREclause that uses a function, other than
MAX, or an operator with an indexed key does not make available the access path that uses the index except with function-based indexes.
DELETEstatements access the parent and child tables. Such an index allows
DELETEs on the parent table without share locking the child table.
DELETEs and the use of the space required to store the index. You might want to experiment by comparing the processing times of the SQL statements with and without indexes. You can measure processing time with the SQL trace facility.
Oracle9i Application Developer's Guide - Fundamentals for more information on the effects of foreign keys on locking
A composite index contains more than one key column. Composite indexes can provide additional advantages over single-column indexes:
Sometimes two or more columns or expressions, each with poor selectivity, can be combined to form a composite index with higher selectivity.
If all columns selected by a query are in a composite index, then Oracle can return these values from the index without accessing the table.
A SQL statement can use an access path involving a composite index if the statement contains constructs that use a leading portion of the index.
This is no longer the case with index skip scans. See "Index Skip Scans".
A leading portion of an index is a set of one or more columns that were specified first and consecutively in the list of columns in the
INDEX statement that created the index. Consider this
xyzcombinations of columns are leading portions of the index
zcombinations of columns are not leading portions of the index
Follow these guidelines for choosing keys for composite indexes:
WHEREclause conditions combined with
ANDoperators, especially if their combined selectivity is better than the selectivity of either key individually.
Of course, consider the guidelines associated with the general performance advantages and trade-offs of indexes described in the previous sections.
Follow these guidelines for ordering keys in composite indexes:
WHEREclauses make up a leading portion.
WHEREclauses more frequently, then be sure to create the index so that the more frequently selected keys make up a leading portion to allow the statements that use only these keys to use the index.
WHEREclauses equally often, then ordering these keys from most selective to least selective in the
INDEXstatement best improves query performance.
WHEREclauses equally often but the data is physically ordered on one of the keys, then place that key first in the composite index.
Even after you create an index, the optimizer cannot use an access path that uses the index simply because the index exists. The optimizer can choose such an access path for a SQL statement only if it contains a construct that makes the access path available. To allow the CBO the option of using an index access path, ensure that the statement contains a construct that makes such an access path available.
In some cases, you might want to prevent a SQL statement from using an access path that uses an existing index. You might want to do this if you know that the index is not very selective and that a full table scan would be more efficient. If the statement contains a construct that makes such an index access path available, then you can force the optimizer to use a full table scan through one of the following methods:
NO_INDEXhint to give the CBO maximum flexibility while disallowing the use of a certain index.
FULLhint to force the optimizer to choose a full table scan instead of an index scan.
AND_EQUALhints to force the optimizer to use one index or a set of listed indexes instead of another.
Chapter 5, "Optimizer Hints" for more information on the
Parallel execution uses indexes effectively. It does not perform parallel index range scans, but it does perform parallel index lookups for parallel nested loop join execution. If an index is very selective (there are few rows for each index entry), then it might be better to use sequential index lookup rather than parallel table scan.
You might want to re-create an index to compact it and minimize fragmented space, or to change the index's storage characteristics. When creating a new index that is a subset of an existing index or when rebuilding an existing index with new storage characteristics, Oracle might use the existing index instead of the base table to improve the performance of the index build.
To avoid calling
However, there are cases where it can be beneficial to use the base table instead of the existing index. Consider an index on a table on which a lot of DML has been performed. Because of the DML, the size of the index can increase to the point where each block is only 50% full, or even less. If the index refers to most of the columns in the table, then the index could actually be larger than the table. In this case, it is faster to use the base table rather than the index to re-create the index.
REBUILD statement to reorganize or compact an existing index or to change its storage characteristics. The
REBUILD statement uses the existing index as the basis for the new one. All index storage statements are supported, such as
STORAGE (for extent allocation),
TABLESPACE (to move the index to a new tablespace), and
INITRANS (to change the initial number of entries).
REBUILD is faster than dropping and re-creating an index, because this statement uses the fast full scan feature. It reads all the index blocks using multiblock I/O, then discards the branch blocks. A further advantage of this approach is that the old index is still available for queries while the rebuild is in progress.
Oracle9i SQL Reference for more information about the
You can coalesce leaf blocks of an index by using the
INDEX statement with the
COALESCE option. This option lets you combine leaf levels of an index to free blocks for reuse. You can also rebuild the index online.
You can use an existing nonunique index on a table to enforce uniqueness, either for
UNIQUE constraints or the unique aspect of a
KEY constraint. The advantage of this approach is that the index remains available and valid when the constraint is disabled. Therefore, enabling a disabled
KEY constraint does not require rebuilding the unique index associated with the constraint. This can yield significant time savings on enable operations for large tables.
Using a nonunique index to enforce uniqueness also lets you eliminate redundant indexes. You do not need a unique index on a primary key column if that column already is included as the prefix of a composite index. You can use the existing index to enable and enforce the constraint. You also save significant space by not duplicating the index. However, if the existing index is partitioned, then the partitioning key of the index must also be a subset of the
UNIQUE key; otherwise, Oracle creates an additional unique index to enforce the constraint.
An enabled novalidated constraint behaves similarly to an enabled validated constraint for new data. Placing a constraint in the enabled novalidated state signifies that any new data entered into the table must conform to the constraint. Existing data is not checked. By placing a constraint in the enabled novalidated state, you enable the constraint without locking the table.
If you change a constraint from disabled to enabled, then the table must be locked. No new DML, queries, or DDL can occur, because there is no mechanism to ensure that operations on the table conform to the constraint during the enable operation. The enabled novalidated state prevents operations violating the constraint from being performed on the table.
An enabled novalidated constraint can be validated with a parallel, consistent-read query of the table to determine whether any data violates the constraint. No locking is performed, and the enable operation does not block readers or writers to the table. In addition, enabled novalidated constraints can be validated in parallel: Multiple constraints can be validated at the same time and each constraint's validity check can be determined using parallel query.
Use the following approach to create tables with constraints and indexes:
NULLconstraints can be unnamed and should be created enabled and validated. All other constraints (
KEY) should be named and created disabled.
TABLEstatement for each constraint, validate all constraints. Do this to primary keys before foreign keys. For example,
CREATE TABLE t (a NUMBER CONSTRAINT apk PRIMARY KEY DISABLE, b NUMBER NOT NULL); CREATE TABLE x (c NUMBER CONSTRAINT afk REFERENCES t DISABLE);
Now you can use Import or Fast Loader to load data into table
CREATE UNIQUE INDEX tai ON t (a); CREATE INDEX tci ON x (c); ALTER TABLE t MODIFY CONSTRAINT apk ENABLE NOVALIDATE; ALTER TABLE x MODIFY CONSTRAINT afk ENABLE NOVALIDATE;
At this point, users can start performing
SELECTs on table
Now the constraints are enabled and validated.
Oracle9i Database Concepts for a complete discussion of integrity constraints
A function-based index includes columns that are either transformed by a function, such as the
UPPER function, or included in an expression, such as
Defining a function-based index on the transformed column or expression allows that data to be returned using the index when that function or expression is used in a
WHERE clause or an
BY clause. Therefore, a function-based index can be beneficial when frequently-executed SQL statements include transformed columns, or columns in expressions, in a
Function-based indexes defined with the
column_name) keywords allow case-insensitive searches. For example, the following index:
facilitates processing queries such as:
To use function-based indexes in queries, you need to set the
To enable function-based indexes for queries, set the
QUERY_REWRITE_ENABLED session parameter to
QUERY_REWRITE_ENABLED can be set to the following values:
QUERY_REWRITE_ENABLED is set to
FALSE, then function-based indexes are not used for obtaining the values of an expression in the function-based index. However, function-based indexes can still be used for obtaining values in real columns.
QUERY_REWRITE_ENABLED is set to
FORCE, Oracle always uses rewrite and does not evaluate the cost before doing so.
FORCE is useful when you know that the query will always benefit from rewrite, when reduction in compile time is important, and when you know that the optimizer may be underestimating the benefits of materialized views.
QUERY_REWRITE_ENABLED is a session-level and also an instance-level parameter.
Setting the value of the
QUERY_REWRITE_INTEGRITY parameter determines how function-based indexes are used,
QUERY_REWRITE_INTEGRITYparameter is set to
ENFORCED(the default), then Oracle uses function-based indexes to derive values of SQL expressions only. This also includes SQL functions.
QUERY_REWRITE_INTEGRITYis set to any value other than
ENFORCED, then Oracle uses the function-based index, even if it is based on a user-defined, rather than SQL, function.
Function-based indexes are an efficient mechanism for evaluating statements that contain functions in
WHERE clauses. You can create a function-based index to store computation-intensive expressions in the index. This permits Oracle to bypass computing the value of the expression when processing
DELETE statements. When processing
UPDATE statements, however, Oracle evaluates the function to process the statement.
For example, if you create the following index:
then Oracle can use it when processing queries such as:
You can also use function-based indexes for linguistic sort indexes that provide efficient linguistic collation in SQL statements.
Oracle treats descending indexes as function-based indexes. The columns marked
DESC are sorted in descending order.
An index-organized table differs from an ordinary table in that the data for the table is held in its associated index. Changes to the table data, such as adding new rows, updating rows, or deleting rows, result only in updating the index. Because data rows are stored in the index, index-organized tables provide faster key-based access to table data for queries that involve exact match or range search or both.
This section describes:
This section describes three aspects of indexing that you must evaluate when deciding whether to use bitmap indexing on a given table:
Bitmap indexes can substantially improve performance of queries that have all of the following characteristics:
WHEREclause contains multiple predicates on low- or medium-cardinality columns.
You can use multiple bitmap indexes to evaluate the conditions on a single table. Bitmap indexes are thus highly advantageous for complex ad hoc queries that contain lengthy
WHERE clauses. Bitmap indexes can also provide optimal performance for aggregate queries and for optimizing joins in star schemas.
Oracle9i Database Concepts for more information on optimizing anti-joins and semi-joins
Bitmap indexes can provide considerable storage savings over the use of B-tree indexes. In databases containing only B-tree indexes, you must anticipate the columns that are commonly accessed together in a single query, and create a composite B-tree index on these columns.
Not only would this B-tree index require a large amount of space, it would also be ordered. That is, a B-tree index on (
gender) is useless for queries that only access
gender. To completely index the database, you must create indexes on the other permutations of these columns. For the simple case of three low-cardinality columns, there are six possible composite B-tree indexes. You must consider the trade-offs between disk space and performance needs when determining which composite B-tree indexes to create.
Bitmap indexes solve this dilemma. Bitmap indexes can be efficiently combined during query execution, so three small single-column bitmap indexes can do the job of six three-column B-tree indexes.
Bitmap indexes are much more efficient than B-tree indexes, especially in data warehousing environments. Bitmap indexes are created not only for efficient space usage but also for efficient execution, and the latter is somewhat more important.
Do not create bitmap indexes on unique key columns. However, for columns where each value is repeated hundreds or thousands of times, a bitmap index typically is less than 25% of the size of a regular B-tree index. The bitmaps themselves are stored in compressed format.
Simply comparing the relative sizes of B-tree and bitmap indexes is not an accurate measure of effectiveness, however. Because of their different performance characteristics, keep B-tree indexes on high-cardinality columns, while creating bitmap indexes on low-cardinality columns.
Bitmap indexes benefit data warehousing applications, but they are not appropriate for OLTP applications with a heavy load of concurrent
DELETEs. In a data warehousing environment, data is maintained usually by way of bulk inserts and updates. Index maintenance is deferred until the end of each DML operation. For example, when you insert 1000 rows, the inserted rows are placed into a sort buffer and then the updates of all 1000 index entries are batched. (This is why
SORT_AREA_SIZE must be set properly for good performance with inserts and updates on bitmap indexes.) Thus, each bitmap segment is updated only once in each DML operation, even if more than one row in that segment changes.
The sorts described previously are regular sorts and use the regular sort area, determined by
DML and DDL statements, such as
TABLE, affect bitmap indexes the same way they do traditional indexes; the consistency model is the same. A compressed bitmap for a key value is made up of one or more bitmap segments, each of which is at most half a block in size (although it can be smaller). The locking granularity is one such bitmap segment. This can affect performance in environments where many transactions make simultaneous updates. If numerous DML operations have caused increased index size and decreasing performance for queries, then you can use the
REBUILD statement to compact the index and restore efficient performance.
A B-tree index entry contains a single rowid. Therefore, when the index entry is locked, a single row is locked. With bitmap indexes, an entry can potentially contain a range of rowids. When a bitmap index entry is locked, the entire range of rowids is locked. The number of rowids in this range affects concurrency. As the number of rowids increases in a bitmap segment, concurrency decreases.
Locking issues affect DML operations and can affect heavy OLTP environments. Locking issues do not, however, affect query performance. As with other types of indexes, updating bitmap indexes is a costly operation. Nonetheless, for bulk inserts and updates where many rows are inserted or many updates are made in a single statement, performance with bitmap indexes can be better than with regular B-tree indexes.
This section discusses performance issues with bitmap indexes.
INDEX hint works with bitmap indexes in the same way as with traditional indexes.
INDEX_COMBINE hint identifies the most cost effective indexes for the optimizer. The optimizer recognizes all indexes that can potentially be combined, given the predicates in the
WHERE clause. However, it might not be cost effective to use all of them. Oracle recommends using
INDEX_COMBINE rather than
INDEX for bitmap indexes, because it is a more versatile hint.
In deciding which of these hints to use, the optimizer includes nonhinted indexes that appear cost effective, as well as indexes named in the hint. If certain indexes are given as arguments for the hint, then the optimizer tries to use some combination of those particular bitmap indexes.
If the hint does not name indexes, then all indexes are considered hinted. Hence, the optimizer tries to combine as many as possible, given the
WHERE clause, without regard to cost effectiveness. The optimizer always tries to use hinted indexes in the plan, regardless of whether it considers them cost effective.
Chapter 5, "Optimizer Hints" for more information on the
When creating bitmap indexes, Oracle needs to consider the theoretical maximum number of rows that will fit in a data block. For this reason, to get optimal performance and disk space usage with bitmap indexes, consider the following tips:
NULLconstraints on all columns that cannot contain null values.
Chapter 9, "Using EXPLAIN PLAN" for more information about bitmap
Use SQL statements with the
TABLE syntax to optimize the mapping of bitmaps to rowids. The
RECORDS_PER_BLOCK clause enables this optimization, and the
RECORDS_PER_BLOCK clause disables it.
RECORDS_PER_BLOCK is enabled, Oracle scans the table and determines the maximum number of records in any block and restricts this table to this maximum number. This enables bitmap indexes to allocate fewer bits for each block and results in smaller bitmap indexes. The block and record allocation restrictions that this statement places on the table are beneficial only to bitmap indexes. Therefore, Oracle does not recommend using this mapping on tables that are not heavily indexed with bitmap indexes.
The rowids used in bitmap indexes on index-organized tables are in a mapping table, not in the base table. The mapping table maintains a mapping of logical rowids (needed to access the index-organized table) to physical rowids (needed by the bitmap index code). Each index-organized table has one mapping table, used by all the bitmap indexes created on that table.
Moving rows in an index-organized table does not make the bitmap indexes built on that index-organized table unusable.
Oracle9i Database Concepts for information on bitmap indexes and index-organized tables
Bitmap indexes index nulls, whereas all other index types do not. Consider, for example, a table with
PARTY columns, on which you want to perform the following query:
Indexing nulls enables a bitmap minus plan where bitmaps for party equal to
NULL are subtracted from state bitmaps equal to
PLAN output looks like the following:
SELECT STATEMENT SORT AGGREGATE BITMAP CONVERSION COUNT BITMAP MINUS BITMAP MINUS BITMAP INDEX SINGLE VALUE STATE_BM BITMAP INDEX SINGLE VALUE PARTY_BM BITMAP INDEX SINGLE VALUE PARTY_BM
NULL constraint exists on party, then the second minus operation (where party is null) is left out, because it is not needed.
The following initialization parameters affect the use of bitmap indexes:
CREATE_BITMAP_AREA_SIZEaffects memory allocated for bitmap creation.
BITMAP_MERGE_AREA_SIZEaffects memory used to merge bitmaps from an index range scan.
SORT_AREA_SIZEaffects memory used when inserting or updating bitmap indexes.
Oracle9i Database Reference for more information on these parameters
If there is at least one bitmap index on the table, then the optimizer considers using a bitmap access path using regular B-tree indexes for that table. This access path can involve combinations of B-tree and bitmap indexes, but might not involve any bitmap indexes at all. However, the optimizer does not generate a bitmap access path using a single B-tree index unless instructed to do so by a hint.
To use bitmap access paths for B-tree indexes, the rowids stored in the indexes must be converted to bitmaps. After such a conversion, the various Boolean operations available for bitmaps can be used. As an example, consider the following query, where there is a bitmap index on column
c1, and regular B-tree indexes on columns
EXPLAIN PLAN FOR SELECT COUNT(*) FROM t WHERE c1 = 2 AND c2 = 6 OR c3 BETWEEN 10 AND 20; SELECT STATEMENT SORT AGGREGATE BITMAP CONVERSION COUNT BITMAP OR BITMAP AND BITMAP INDEX c1_ind SINGLE VALUE BITMAP CONVERSION FROM ROWIDS INDEX c2_ind RANGE SCAN BITMAP CONVERSION FROM ROWIDS SORT ORDER BY INDEX c3_ind RANGE SCAN
COUNT option for the
CONVERSION row source counts the number of rows matching the query. There are also conversions from rowids in the plan to generate bitmaps from the rowids retrieved from the B-tree indexes. The
BY sort appears in the plan because the conditions on column
c3 result in the return of more than one list of rowids from the B-tree index. These lists are sorted before being converted into a bitmap.
Bitmap indexes have the following restrictions:
SORTED_INDEXflag does not apply.
In addition to a bitmap index on a single table, you can create a bitmap join index, which is a bitmap index for the join of two or more tables. A bitmap join index is a space-saving way to reduce the volume of data that must be joined, by performing restrictions in advance. For each value in a column of a table, a bitmap join index stores the rowids of corresponding rows in another table. In a data warehousing environment, the join condition is an equi-inner join between the primary key column(s) of the dimension tables and the foreign key column(s) in the fact table.
Bitmap join indexes are much more efficient in storage than materialized join views, an alternative for materializing joins in advance. This is because the materialized join views do not compress the rowids of the fact tables.
Oracle9i Data Warehousing Guide for examples and restrictions of bitmap join indexes
Domain indexes are built using the indexing logic supplied by a user-defined indextype. An indextype provides an efficient mechanism to access data that satisfy certain operator predicates. Typically, the user-defined indextype is part of an Oracle option, like the Spatial option. For example, the
SpatialIndextype allows efficient search and retrieval of spatial data that overlap a given bounding box.
The cartridge determines the parameters you can specify in creating and maintaining the domain index. Similarly, the performance and storage characteristics of the domain index are presented in the specific cartridge documentation.
Refer to the appropriate cartridge documentation for information such as the following:
Oracle Spatial User's Guide and Reference for information about the
Clusters are groups of one or more tables that are physically stored together because they share common columns and usually are used together. Because related rows are physically stored together, disk access time improves.
To create a cluster, use the
Oracle9i Database Concepts for more information on clusters
Follow these guidelines when deciding whether to cluster tables:
Consider the benefits and drawbacks of clusters with respect to the needs of the application. For example, you might decide that the performance gain for join statements outweighs the performance loss for statements that modify cluster key values. You might want to experiment and compare processing times with the tables both clustered and stored separately.
Oracle9i Database Administrator's Guide for more information on creating clusters
Hash clusters group table data by applying a hash function to each row's cluster key value. All rows with the same cluster key value are stored together on disk. Consider the benefits and drawbacks of hash clusters with respect to the needs of the application. You might want to experiment and compare processing times with a particular table as it is stored in a hash cluster, and as it is stored alone with an index.
Follow these guidelines for choosing when to use hash clusters:
WHEREclauses, if the
WHEREclauses contain equality conditions that use the same column or combination of columns. Designate this column or combination of columns as the cluster key.
Storing a single table in a hash cluster can be useful, regardless of whether the table is joined frequently with other tables, as long as hashing is appropriate for the table based on the points in this list.