Sub topic 1
Why Indexes Exist and What They Cost
An index is a separate database structure that helps Oracle find table rows without examining every row. An index stores column values in an organized form and keeps a reference to each matching row. In Oracle, that reference is usually a ROWID, which identifies the row's physical location.
Prerequisites
This chapter assumes that you know basic SELECT and WHERE syntax, can create tables and insert rows in Oracle, and understand that the optimizer chooses an execution plan. You should also know that a table row can be located by its ROWID.
Why an Index Can Help
Suppose orders contains ten million rows and a query asks for one order with a particular order_id. Without a suitable index, Oracle may read many or all table blocks to find the row. This is a full table scan.
An index keeps values in a searchable structure. Oracle can search the index for the requested value, obtain the matching ROWID, and visit only the needed table rows. This is often called an index access path.
CREATE TABLE orders (
order_id NUMBER PRIMARY KEY,
customer_id NUMBER NOT NULL,
status VARCHAR2(10) NOT NULL,
order_date DATE NOT NULL
);
CREATE INDEX orders_customer_ix
ON orders (customer_id);
SELECT order_id, status
FROM orders
WHERE customer_id = 42;The primary key declaration normally creates or uses a unique index to enforce that each order_id is different. The explicitly created index supports searches by customer_id.
The Cost of an Index
An index does not make every operation faster. It has several costs:
- It uses storage for its keys and row references.
- An
INSERTmust add an entry to every relevant index. - An
UPDATEmay remove an old entry and add a new one when an indexed column changes. - A
DELETEmust remove the row's entries from every relevant index. - Oracle must maintain index blocks and may need extra work when blocks become full or fragmented.
- The optimizer needs current statistics to estimate whether using the index is worthwhile.
For a query that returns a small fraction of a table, an index is often useful. For a query that returns most of the table, reading the table sequentially can cost less than finding many index entries and then visiting many table blocks. Therefore, creating an index does not force Oracle to use it.
| Workload | Likely index benefit | Main cost |
|---|---|---|
| Selective lookups on frequently queried columns | High | Extra storage and DML maintenance |
| Reports reading most rows | Often low | Index may be ignored |
| Tables with frequent inserts and updates | Mixed | More work for each DML operation |
| Rarely queried columns | Low | Unused storage and maintenance |
Choosing an Index Deliberately
Index columns used in common search conditions, joins, or ordering operations when measured queries benefit from them. Avoid indexing every column. Each additional index increases write work and gives the optimizer another object to consider.
A useful design starts with the workload:
- Identify queries that are slow or run frequently.
- Check their current execution plans.
- Create the smallest index that supports the access pattern.
- Measure both query performance and DML performance.
- Keep the index only if the overall workload improves.
A B-tree index is Oracle's general-purpose index type and is the normal starting point. Bitmap and function-based indexes solve more specialized problems, discussed in later chapters.
Exercise
Why can adding an index improve a SELECT statement but slow down INSERT statements?
Show answer
The SELECT can find rows through the index instead of scanning the table. Each INSERT must also add entries to the index, which creates extra work and uses additional storage.