Sub topic 6
Checking Index Use with EXPLAIN PLAN
Assume you know SELECT, WHERE, and basic table and index creation. This chapter also assumes that Oracle's optimizer chooses an execution plan for each SQL statement. An execution plan is the set of operations Oracle expects to use to produce the result.
What EXPLAIN PLAN Shows
EXPLAIN PLAN asks Oracle to calculate a plan without running the query. The plan can show whether Oracle expects to use an index, scan the table, join tables, sort rows, or perform other operations.
For example, suppose orders_cust_date_ix is a B-tree index on (customer_id, order_date):
EXPLAIN PLAN FOR
SELECT order_id
FROM orders
WHERE customer_id = 42
AND order_date >= DATE '2026-01-01';
SELECT *
FROM TABLE(DBMS_XPLAN.DISPLAY);DBMS_XPLAN.DISPLAY reads the plan that EXPLAIN PLAN stored and formats it for you. A typical result might contain:
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost |
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 8 | 3 |
|* 1 | TABLE ACCESS BY INDEX ROWID BATCHED | ORDERS | 8 | 3 |
|* 2 | INDEX RANGE SCAN | ORDERS_CUST_DATE_IX| 8 | 2 |
--------------------------------------------------------------------------------The operation at the bottom usually supplies row locations to the operations above it. INDEX RANGE SCAN means Oracle found a range of keys in a B-tree index. It then used the index entries to locate table rows.
The asterisk beside an operation means that the plan has a predicate for that operation. The predicate section below the plan shows the conditions Oracle applies.
Recognizing Common Access Paths
The access path is the method Oracle uses to find table rows. These are common operations:
| Plan operation | Meaning | Typical situation |
|---|---|---|
| `INDEX UNIQUE SCAN` | Finds one key in a unique index | Equality lookup on a primary key |
| `INDEX RANGE SCAN` | Finds a range of index keys | Equality or range condition on a B-tree index |
| `TABLE ACCESS BY INDEX ROWID` | Fetches table rows identified by an index | The query needs columns not stored in the index |
| `TABLE ACCESS FULL` | Reads the table blocks directly | Many rows qualify, or no useful index exists |
A full table scan is not automatically a problem. If a query needs most rows, reading the table sequentially can cost less than finding many index entries and then visiting table blocks one by one.
How Composite Indexes Affect the Plan
For an index on (customer_id, order_date), a predicate on customer_id gives Oracle a leading key value. Oracle can then search the relevant order_date range.
A predicate only on order_date usually cannot use this index efficiently because order_date is not the leading column. Column order is therefore part of index design, not just table decoration.
The optimizer also considers selectivity, which describes how much a condition narrows the result. A condition that matches very few rows is more likely to benefit from an index. A condition that matches a large portion of the table may lead to a full table scan.
Diagram: Choosing an Access Path
Testing an Index Safely
To test the effect of an index without dropping it, make it invisible:
ALTER INDEX orders_cust_date_ix INVISIBLE;
-- Run the test workload and compare its plans.
ALTER INDEX orders_cust_date_ix VISIBLE;An invisible index remains maintained, but the optimizer normally ignores it. This lets you compare plans without rebuilding the index. Applications that explicitly name an index can behave differently, so test the real workload.
Exercise
A plan shows TABLE ACCESS FULL for a query with a condition on order_date. The table has an index on (customer_id, order_date). What is one likely reason Oracle does not use that index efficiently?
Show answer
order_date is not the leading column. The index can usually narrow the search efficiently only when the query also supplies the leading customer_id value.