Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to choose and optimize oracle indexes? [closed]

I would like to know if there are general rules for creating an index or not. How do I choose which fields I should include in this index or when not to include them?

I know its always depends on the environment and the amount of data, but I was wondering if we could make some globally accepted rules about making indexes in Oracle.

like image 474
guigui42 Avatar asked Oct 17 '08 14:10

guigui42


People also ask

How do you optimize indexes?

The optimization of SQL indexes can be done by using SQL profiler, running Index Tuning Wizard, using SQL Query Analyzer and by defragmentation of indexes. For a large database, defragment the indexes is the best practice to optimize SQL server indexes.

Which index is better in Oracle?

A function-based index increases the variety of ways in which you can access data. Note: The index is more effective if you gather statistics for the table or schema, using the procedures in the DBMS_STATS package. The index cannot contain any null values.

How does index improve query performance in Oracle?

Indexes are used in Oracle to provide quick access to rows in a table. Indexes provide faster access to data for operations that return a small portion of a table's rows. Although Oracle allows an unlimited number of indexes on a table, the indexes only help if they are used to speed up queries.


2 Answers

The Oracle documentation has an excellent set of considerations for indexing choices: http://download.oracle.com/docs/cd/B28359_01/server.111/b28274/data_acc.htm#PFGRF004

Update for 19c: https://docs.oracle.com/en/database/oracle/oracle-database/19/tgdba/designing-and-developing-for-performance.html#GUID-99A7FD1B-CEFD-4E91-9486-2CBBFC2B7A1D

Quoting:

  • Consider indexing keys that are used frequently in WHERE clauses.

  • Consider indexing keys that are used frequently to join tables in SQL statements. For more information on optimizing joins, see the section "Using Hash Clusters for Performance".

  • Choose index keys that have high selectivity. The selectivity of an index is the percentage of rows in a table having the same value for the indexed key. An index's selectivity is optimal if few rows have the same value. Note: 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.

  • Do not use standard B-tree indexes on keys or expressions with few distinct values. Such keys or expressions usually have poor selectivity and therefore do not optimize performance unless the frequently selected key values appear less frequently than the other key values. You can use bitmap indexes effectively in such cases, unless the index is modified frequently, as in a high concurrency OLTP application.

  • Do not index columns that are modified frequently. UPDATE statements that modify indexed columns and INSERT and DELETE statements 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.

  • Do not index keys that appear only in WHERE clauses with functions or operators. A WHERE clause that uses a function, other than MIN or MAX, or an operator with an indexed key does not make available the access path that uses the index except with function-based indexes.

  • Consider indexing foreign keys of referential integrity constraints in cases in which a large number of concurrent INSERT, UPDATE, and DELETE statements access the parent and child tables. Such an index allows UPDATEs and DELETEs on the parent table without share locking the child table.

  • When choosing to index a key, consider whether the performance gain for queries is worth the performance loss for INSERTs, UPDATEs, and 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.

like image 56
David Aldridge Avatar answered Sep 19 '22 19:09

David Aldridge


There are some things you should always index:

  • Primary Keys - these are given an index automatically (unless you specify a suitable existing index for Oracle to use)
  • Unique Keys - these are given an index automatically (ditto)
  • Foreign Keys - these are not automatically indexed, but you should add one to avoid performance issues when the constraints are checked

After that, look for other columns that are frequently used to filter queries: a typical example is people's surnames.

like image 20
Tony Andrews Avatar answered Sep 20 '22 19:09

Tony Andrews