Sub topic 17
The Cost of Indexes on DML and When Not to Add One
This chapter assumes you know basic SELECT, INSERT, UPDATE, and DELETE statements, and that you can read an Oracle execution plan. An index can make reads faster, but every index also adds work to data changes.
Every index has a maintenance cost
A B-tree index stores ordered key values and row references. When a table changes, Oracle must keep each related index consistent.
| Table operation | Work added by an index |
|---|---|
| `INSERT` | Adds an entry to every applicable index |
| `DELETE` | Removes an entry from every applicable index |
| `UPDATE` of an indexed column | Usually removes the old entry and adds a new entry |
| `UPDATE` of a non-indexed column | Usually does not change that index |
Suppose orders has five indexes. Inserting one order may require one table change plus five index changes. The statement may still be fast, but it does more work than it would without those indexes.
Indexes also consume disk space and buffer cache. A wide index key, such as several long VARCHAR2 columns, can require substantial storage. More index blocks can also mean more physical reads when the index is used.
flowchart TD
DML[INSERT UPDATE or DELETE] --> T[Change table block]
DML --> I1[Maintain index 1]
DML --> I2[Maintain index 2]
DML --> I3[Maintain index 3]
I1 --> C[More redo and undo]
I2 --> C
I3 --> CMeasure the read benefit
An index is worthwhile when the time saved by important queries is greater than the cost added to the workload. Check the actual workload rather than adding an index for every column used in a WHERE clause.
A useful comparison is:
- Identify a slow or important query.
- Check its execution plan and runtime.
- Create a candidate index in a test environment.
- Gather suitable optimizer statistics if needed.
- Recheck the plan and runtime.
- Measure insert, update, and delete performance as well.
The optimizer may choose a full table scan even when an index exists. This can be correct. For example, a query that returns most rows may read fewer blocks by scanning the table than by reading many index entries and then visiting the table rows.
Cases where an index may not help
Avoid adding an index when its benefit is small or its maintenance cost is high.
- A very small table may be faster to scan than to search through an index.
- A column with very few distinct values may not filter enough rows for a normal index to help.
- A table with heavy insert or update traffic may suffer from unnecessary index maintenance.
- An index that duplicates another index wastes space and DML effort.
- A rarely executed query may not justify a permanent index.
- An index that is never chosen by important plans may be a candidate for removal, after careful verification.
Low selectivity alone does not prove that an index is useless. The optimizer also considers table size, clustering, histograms, partitioning, and the rest of the predicate.
Remove redundancy carefully
An index on (customer_id, order_date) can support searches beginning with customer_id. A separate index on (customer_id) may therefore be redundant for many workloads. However, the shorter index might still be useful if it is much smaller or supports a particular constraint or plan.
Do not drop an index based on one query. Check application code, constraints, scheduled jobs, and representative execution plans first.
Exercise
Exercise
A table receives thousands of inserts per minute, but an index is used only by a report that runs once each week. What should you investigate before keeping the index?
Show answer
Measure the report's benefit, the index's storage and maintenance cost, whether another index can support the report, and whether the report can use a different design or schedule.