Sub topic 13
Covering indexes and avoiding table access
This chapter assumes you can read a plan from DBMS_XPLAN.DISPLAY. It also assumes you know how a composite index is searched from its leading column. Both were covered in earlier chapters.
Why table access costs more than the index search
A typical index lookup has two phases. Oracle first walks the B-tree and collects ROWIDs from the leaf entries that match. It then fetches each row from the table with TABLE ACCESS BY INDEX ROWID, because the select list needs columns the index does not hold.
The index phase is cheap. The matching entries sit next to each other in sorted order, so they come from a few adjacent leaf blocks. The table phase can be expensive. The matching rows may be scattered across many table blocks, and each block may need its own single-block read. How badly they scatter is the index's clustering factor. A query that matches 5,000 index entries can therefore cost far more in table reads than in index reads.
A covering index removes the second phase. Every column the query references, in the select list and in every other clause, is stored in the index. Oracle answers the query from the index leaf blocks alone and never touches the table.
A query that needs the table
The earlier chapters used this index:
CREATE INDEX orders_cust_date_ix
ON orders (customer_id, order_date);Suppose a report needs the order amount as well:
EXPLAIN PLAN FOR
SELECT order_date, total_amount
FROM orders
WHERE customer_id = 42
AND order_date >= DATE '2026-01-01'
AND order_date < DATE '2026-02-01';
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);The plan has this shape. Costs, row counts and the BATCHED suffix depend on your version and statistics.
| Id | Operation | Name |
| 0 | SELECT STATEMENT | |
| 1 | TABLE ACCESS BY INDEX ROWID BATCHED| ORDERS |
|* 2 | INDEX RANGE SCAN | ORDERS_CUST_DATE_IX |
Predicate Information (identified by operation id):
2 - access("CUSTOMER_ID"=42 AND "ORDER_DATE">=... AND "ORDER_DATE"<...)The index finds the rows efficiently, but total_amount is not in it. Step 1 must visit the table for every matching entry.
Making the index cover the query
Add the missing column at the end of the index:
CREATE INDEX orders_cust_date_amt_ix
ON orders (customer_id, order_date, total_amount);The same query now needs only the index:
| Id | Operation | Name |
| 0 | SELECT STATEMENT | |
|* 1 | INDEX RANGE SCAN| ORDERS_CUST_DATE_AMT_IX |The TABLE ACCESS line is gone, and that is the signal that the index covers the query. Oracle still searches by customer_id and order_date. total_amount is stored in each leaf entry but plays no part in the search.
What counts as "every referenced column"
Check every clause, not only the select list.
- Select list. Each selected column must be in the index.
- WHERE. Columns used only in filters count too. Oracle can test a predicate on a trailing index column inside the index, and the plan then shows it as a filter predicate on the index step. It saves table visits for rows that would fail the test.
- ORDER BY, GROUP BY and joins. Any column used there must be in the index.
- Expressions. If the query uses
UPPER(last_name), a function-based index on that expression covers it. A plain index onlast_namealso covers it, because Oracle can readlast_namefrom the index and computeUPPERon the result. The reverse does not hold. A query selectinglast_nameis not covered by an index onUPPER(last_name)alone.
SELECT * almost never works with a covering index, because the index would have to contain every column of the table. List only the columns you need.
Index-only operations beyond the range scan
A covering index enables other access paths. Which one Oracle picks depends on the predicates and the cost estimate.
| Operation | When it appears |
|---|---|
| `INDEX RANGE SCAN` | The query constrains the leading column, and all needed columns are in the index. |
| `INDEX FULL SCAN` | Oracle reads the whole index in key order, one block at a time. It can avoid a sort, but it is not a fast scan. |
| `INDEX FAST FULL SCAN` | Oracle reads the whole index with multiblock reads, in no guaranteed order. It treats the index as a thin copy of the table. |
| `INDEX SKIP SCAN` | The leading column is not constrained but has few distinct values. |
A fast full scan can answer a query even when the leading column is not constrained. For example, SELECT customer_id, SUM(total_amount) FROM orders GROUP BY customer_id can scan the narrow index in place of the wider table.
COUNT(*) and the NULL rule
Chapter "NULLs and what B-tree indexes store" established that a B-tree index omits entries whose indexed columns are all NULL. This matters for counting:
SELECT COUNT(*) FROM orders;Oracle can answer this from an index only if the index is guaranteed to contain every row. That requires at least one indexed column to be NOT NULL. If it is, Oracle can count with INDEX FAST FULL SCAN on the smallest suitable index. If every indexed column is nullable, the index may be missing rows, so Oracle must scan the table instead.
An index with a nullable leading column but a NOT NULL second column still contains every row, so it can serve here.
The cost of widening an index
Each extra column has a price:
- Size. Every leaf entry grows. A larger index needs more blocks, more buffer cache, and more I/O for every scan.
- DML. An update to any indexed column must also modify the index. Adding a frequently updated column such as
statusto a covering index makes every status change touch the index. - Single-purpose indexes. An index widened to cover one report query may help only that query.
Oracle has no INCLUDE clause. A column that only supplies data still goes into the key, and it counts against the key-size limit.
Verifying the result
Do not assume that a wider index removed table access. Run the plan again, then confirm the effect with actual work done. In SQL*Plus, SET AUTOTRACE TRACEONLY STATISTICS reports consistent gets for the statement. A real covering index drops that number clearly, because the table-block reads disappear. If TABLE ACCESS BY INDEX ROWID is still in the plan, some referenced column is still missing from the index. Compare the select list and every other clause with the index definition, column by column.
Exercise
A table invoices has an index on (customer_id, invoice_date). The query SELECT invoice_date, status FROM invoices WHERE customer_id = 7 ORDER BY invoice_date shows TABLE ACCESS BY INDEX ROWID BATCHED above an INDEX RANGE SCAN. How would you make the index cover this query, which operation should replace the plan, and what is the trade-off if status changes often?
Show answer
Create the index on (customer_id, invoice_date, status). This keeps customer_id as the search column and invoice_date as the sort column, so the ORDER BY is still satisfied by index order. Adding status at the end puts every referenced column in the index, so the plan should show only INDEX RANGE SCAN with no table access. The trade-off is that every update to status must also modify the index entry, which slows DML and enlarges the index.