Sub topic 17
The Cost of Indexes on DML and When Not to Add One
This chapter assumes you can read an execution plan with DBMS_XPLAN, and you know how a B-tree index is searched and what composite and function-based indexes are. Earlier chapters showed how to make a query use an index. This chapter covers the other side: every index you add makes writes slower, and some indexes are not worth having.
Every index is a second copy of your data
An index is a separate segment. It holds the indexed column values, in sorted order, plus a ROWID for each row. Oracle keeps it in step with the table on every change, inside the same transaction.
| Statement | Work on each index of the table |
|---|---|
| `INSERT` | Adds one entry in the correct leaf block. This may split a full block. |
| `DELETE` | Removes the entry from every index on the table. |
| `UPDATE` of an indexed column | Removes the old entry and adds a new one at the new sorted position. |
| `UPDATE` of a column that is in no index | No index work. |
Index maintenance has four costs:
- CPU and block reads. Oracle must navigate each index from root to leaf to find the right place. A table with six indexes does seven structure updates per inserted row.
- Redo and undo. Each index change generates redo and undo. More redo means more log writing at commit, a larger archive log volume, and more work for standby databases.
- Block splits. Inserting into a full leaf block splits it. Random key values, such as GUIDs, split blocks across the whole index. Increasing values such as sequence numbers concentrate inserts in the rightmost block, which can cause contention in busy systems.
- Space. Indexes take storage, backup time, and buffer cache that other data could use.
Queries gain from indexes only when the index lets Oracle read fewer blocks. A write always pays.
Measuring the cost yourself
You can see the effect by comparing redo generation with and without indexes. This script needs SELECT privilege on V$MYSTAT and V$STATNAME.
CREATE TABLE order_items_demo (
item_id NUMBER,
order_id NUMBER,
product_id NUMBER,
status VARCHAR2(10),
created DATE
);
-- Redo generated so far in this session
SELECT s.value AS redo_bytes
FROM v$mystat s
JOIN v$statname n ON n.statistic# = s.statistic#
WHERE n.name = 'redo size';
INSERT INTO order_items_demo
SELECT LEVEL, MOD(LEVEL, 5000), MOD(LEVEL, 300), 'NEW',
DATE '2026-01-01' + MOD(LEVEL, 365)
FROM dual
CONNECT BY LEVEL <= 100000;
-- Run the redo query again and subtract the first value
SELECT s.value AS redo_bytes
FROM v$mystat s
JOIN v$statname n ON n.statistic# = s.statistic#
WHERE n.name = 'redo size';
ROLLBACK;
CREATE INDEX items_order_ix ON order_items_demo (order_id);
CREATE INDEX items_product_ix ON order_items_demo (product_id, created);
CREATE INDEX items_status_ix ON order_items_demo (status);
-- Repeat the same three steps: redo query, INSERT, redo query.
-- The difference is noticeably larger with three indexes.The exact numbers depend on your version, block size, and settings. The point is the ratio between the indexed and unindexed runs.
Updates behave in the same way. With the three indexes in place, the first statement below touches items_status_ix. The second touches no index, so Oracle does only table work and its undo.
-- Changes an indexed column: table row plus index delete and insert
UPDATE order_items_demo SET status = 'SHIPPED' WHERE order_id = 42;
-- Changes a column that no index contains
ALTER TABLE order_items_demo ADD (notes VARCHAR2(100));
UPDATE order_items_demo SET notes = 'checked' WHERE order_id = 42;When an index does not pay for itself
Not every query benefits from an index, even if the predicate is sargable.
- Low selectivity. If a predicate matches a large fraction of the table, Oracle reads most table blocks anyway. Each index entry then costs a single-block read of the table, and a multiblock full scan is cheaper. An index on a column with a few values, such as
statuswhere 90 percent of rows are'NEW', rarely helps queries for the common value. - Small tables. If a table fits in a few blocks, a full scan costs about the same as walking an index.
- Write-heavy tables. A logging or staging table that takes thousands of inserts per second and is read rarely should carry the minimum indexes, often only the primary key.
- Redundant indexes. An index on
(customer_id)is usually unnecessary if(customer_id, order_date)exists, because the composite index serves the same lookups through its leading column. Check before you drop it. A unique constraint or a foreign key may depend on the narrower index, and the narrower index is smaller to scan. - Indexes no query uses. They cost writes and space and return nothing.
Two cases look like exceptions but are not. First, a skewed column with a histogram can still benefit from an index for its rare values, for example status = 'FAILED' when failures are 0.1 percent of rows. Second, an index on a foreign key column is often worth its cost even if no query filters on it, because without it Oracle takes stronger locks on the child table when you delete or update a parent key. Judge foreign-key indexes by your delete and update patterns.
Finding indexes that are not used
Oracle can record whether an index was used. Turn monitoring on, run a representative workload, then check.
ALTER INDEX items_status_ix MONITORING USAGE;
-- ... let the normal workload run for a full business cycle ...
SELECT index_name, monitoring, used, start_monitoring
FROM v$object_usage
WHERE index_name = 'ITEMS_STATUS_IX';
ALTER INDEX items_status_ix NOMONITORING USAGE;V$OBJECT_USAGE shows indexes of the current schema only, and USED is a simple YES or NO flag. Run the monitor across month-end, quarter-end, and batch windows. An index used once a quarter by a report looks unused until then.
A safer first step than dropping is making the index invisible. The optimizer ignores an invisible index, but Oracle still maintains it, so you can bring it back instantly.
ALTER INDEX items_status_ix INVISIBLE;
-- Run the workload and watch for plan regressions.
-- If something gets slower, reverse it at once:
ALTER INDEX items_status_ix VISIBLE;
-- If nothing changed after a full cycle:
DROP INDEX items_status_ix;Bulk loads
For a large one-time load into a table, per-row index maintenance is the slowest way to do it. Common options:
- Load first with a direct-path insert (
INSERT /*+ APPEND */), then create the indexes afterward. Building an index from sorted data in one pass is much faster than adding entries one at a time. - Mark non-unique indexes unusable with
ALTER INDEX ... UNUSABLE, load, thenALTER INDEX ... REBUILD. Oracle skips unusable indexes during DML by default (SKIP_UNUSABLE_INDEXES = TRUE). Indexes that enforce a unique or primary key constraint are the exception and must stay usable for the load to work. - Plan for the time of rebuilds and the window during which queries cannot use the index.
A practical checklist before adding an index
- Confirm in the plan that a full scan or a poor access path is the actual problem.
- Check whether an existing index can serve the query, or be extended by one column.
- Estimate how many rows the predicate returns relative to the table size.
- Count the indexes already on the table and how often it is written.
- Test the DML throughput with the new index, not just the query speed.
- Add it. Re-run the plan and confirm the index is used.
- Review index usage after a full business cycle.
Exercise
A table audit_log receives 2,000 inserts per second and is queried a few times a day by one report that filters on event_type, where 40 percent of rows have the value 'LOGIN'. A colleague proposes indexing event_type. What is your recommendation and why?
Show answer
Do not add the index. The write rate is high, so every insert would pay for another index entry plus redo and undo. The column has low selectivity, since the report's common value matches 40 percent of rows, so the optimizer would probably choose a full scan anyway. The report runs rarely, so the read benefit is small. If a rare event type matters, check whether a histogram and an index on that value help, and test the insert cost first.
Exercise
You think items_status_ix is unused and you want to remove it safely. List the steps in order.
Show answer
1. Enable ALTER INDEX items_status_ix MONITORING USAGE and watch V$OBJECT_USAGE across a full business cycle, including month-end jobs. 2. Make the index invisible with ALTER INDEX items_status_ix INVISIBLE and watch for plan regressions. 3. If any query slows down, run ALTER INDEX items_status_ix VISIBLE. 4. If nothing changes after a full cycle, run DROP INDEX items_status_ix.