Sub topic 6
Checking index use with EXPLAIN PLAN
You can create an index in seconds. Knowing whether Oracle uses it takes a little more work. This chapter shows how to produce an execution plan, read its operations, and decide whether a query is using the index you expect.
It assumes you can write SELECT queries with WHERE clauses. It also assumes you know that the optimizer picks an execution plan, and you have the orders table and the orders_cust_date_ix index from the earlier chapters.
What EXPLAIN PLAN does
EXPLAIN PLAN asks the optimizer how it would run a statement. It does not run the statement. It writes the proposed plan as rows into a plan table, normally PLAN_TABLE. Oracle provides this table as a global temporary table in recent releases, so you do not need to create it. DBMS_XPLAN.DISPLAY then formats those rows as text.
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);Use the two statements together. The first stores the plan, and the second prints the most recent one from your session. If you run another EXPLAIN PLAN in between, you will print the wrong plan.
A typical result looks like this. Your numbers will differ.
Plan hash value: 3921507714
--------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 12 | 180 | 4 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID BATCHED| ORDERS | 12 | 180 | 4 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | ORDERS_CUST_DATE_IX | 12 | | 2 (0)| 00:00:01 |
--------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("CUSTOMER_ID"=42 AND "ORDER_DATE">=TO_DATE(' 2026-01-01 00:00:00',
'syyyy-mm-dd hh24:mi:ss'))Reading the plan
A plan is a tree. Indentation shows the parent and child relationship. Oracle runs the most deeply indented operations first. Each child passes its rows up to its parent. In the plan above, Id 2 finds matching ROWIDs in the index. Id 1 then uses those ROWIDs to fetch table rows. Id 0 returns the result.
The columns that matter most:
| Column | Meaning |
|---|---|
| Id | Row number. A `*` means the operation has predicate information below the plan. |
| Operation | What Oracle does at this step. |
| Name | The object touched, such as an index or a table. |
| Rows | The optimizer's estimate of rows produced, not a measured count. |
| Cost | A relative number the optimizer uses to compare candidate plans. It is not a time. |
The Rows and Cost values are estimates based on statistics. Do not compare costs between different queries. Use them only to understand why the optimizer preferred one plan for one query.
Operations that tell you how an index is used
| Operation | What it means |
|---|---|
| `INDEX UNIQUE SCAN` | An equality lookup on a unique or primary key index. It returns at most one row. |
| `INDEX RANGE SCAN` | Oracle descends the B-tree to a starting entry, then reads entries in order until the condition stops matching. |
| `INDEX SKIP SCAN` | Oracle probes a composite index without a condition on its leading column. It works only when the leading column has few distinct values. |
| `INDEX FULL SCAN` | Oracle reads the whole index in order. It can avoid a sort. |
| `INDEX FAST FULL SCAN` | Oracle reads the whole index in storage order, using multiblock reads. The result is unordered. |
| `TABLE ACCESS BY INDEX ROWID` | Oracle fetches table rows using ROWIDs from an index. |
| `TABLE ACCESS FULL` | Oracle reads every block of the table up to the high-water mark. |
If every column the query needs is in the index, Oracle can skip the table step. In that case the plan has no TABLE ACCESS BY INDEX ROWID line. For example, a query selecting only customer_id and order_date with the same filter would show only the INDEX RANGE SCAN.
A TABLE ACCESS FULL is not automatically a problem. If the query needs a large share of the table's rows, a full scan with multiblock reads can beat thousands of single-block index lookups.
Access and filter predicates
The Predicate Information section is the most useful part of the plan for index work. It tells you how each condition is applied.
access(...)means the condition is used to locate rows. On an index operation, it limits which index entries are read.filter(...)means rows are read first and then tested. On an index operation, the condition is checked against entries already read. On a table operation, it is checked after the rows are fetched.
Consider a query that filters only on the second column of the composite index:
EXPLAIN PLAN FOR
SELECT order_id
FROM orders
WHERE order_date >= DATE '2026-01-01';
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);Because order_date is not the leading column, the optimizer cannot do a normal range scan on orders_cust_date_ix. Depending on statistics, you may see TABLE ACCESS FULL, or an INDEX SKIP SCAN, or an index scan with the date condition shown as a filter. In every case, read the predicates to see which conditions actually narrow the search.
A second example is a function applied to an indexed column. Before you create orders_email_upper_ix, run this:
EXPLAIN PLAN FOR
SELECT order_id
FROM orders
WHERE UPPER(email) = '[email protected]';
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);The plan shows TABLE ACCESS FULL on ORDERS, with filter(UPPER("EMAIL")='[email protected]'). A plain index on email cannot answer a condition on UPPER(email). After you create the function-based index, the same statement should show an INDEX RANGE SCAN on ORDERS_EMAIL_UPPER_IX.
Why an expected index is not used
When a full scan appears where you wanted an index, check these causes in order.
- A function or expression on the column.
WHERE UPPER(email) = ...andWHERE TRUNC(order_date) = ...cannot use a plain index on the bare column. Rewrite the condition or create a function-based index. - An implicit type conversion. If
emailwere aVARCHAR2column and you compared it to a number, Oracle would wrap the column inTO_NUMBER. The predicate section showsINTERNAL_FUNCTIONorTO_NUMBERon the column. Compare values of the matching type. - The leading column is missing. A composite index on
(customer_id, order_date)supports a range scan only when the condition constrainscustomer_id. - Low selectivity. If the condition matches a large fraction of the table, the full scan may truly be cheaper.
- Stale or missing statistics. The optimizer's
Rowsestimate drives its choice. If the estimate is far from reality, the plan can be wrong. - A NULL problem. B-tree indexes do not store entries whose indexed columns are all NULL. A condition like
WHERE email IS NULLon a single-column index onemailcannot use that index.
Estimated plans versus actual plans
EXPLAIN PLAN shows the plan the optimizer would choose now, for your session. It has two limits:
- It treats bind variables as unknown values. The plan can differ from the one used when the query actually runs with peeked bind values.
- It cannot show how many rows each step actually returned.
To see the plan that was really used, run the statement and then display its cursor plan with runtime statistics:
SELECT /*+ GATHER_PLAN_STATISTICS */ order_id
FROM orders
WHERE customer_id = 42
AND order_date >= DATE '2026-01-01';
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(format => 'ALLSTATS LAST'));Run the second statement immediately after the first, in the same session. The output adds Starts, E-Rows (estimated rows), A-Rows (actual rows), A-Time, and Buffers columns. A large gap between E-Rows and A-Rows is the strongest sign of a statistics problem. Buffers shows how many blocks each step read, which is a direct measure of work.
If DISPLAY_CURSOR returns an error or no data, your user probably lacks access to the V$ views it reads, such as V$SESSION and V$SQL_PLAN. Ask a DBA for the needed privileges, for example SELECT_CATALOG_ROLE.
Working habits
- Check the plan before and after you create or drop an index. Compare both plans, not only the new one.
- Test with realistic data volumes. On a table with 100 rows, a full scan is nearly always cheapest, and the plan tells you nothing about production.
- Use the same literals or bind types as the real application.
- Do not read a lower cost as proof of a faster query. Confirm with
A-Rows,Buffers, and elapsed time.
Exercises
Exercise
A plan shows TABLE ACCESS BY INDEX ROWID above an INDEX RANGE SCAN on orderscustdate_ix. Which operation runs first, and why does the table access step appear at all?
Show answer
The INDEX RANGE SCAN runs first because it is the most deeply indented child. It passes ROWIDs to its parent. The table access step appears because the query selects order_id, which is not stored in the index, so Oracle must fetch the table rows by ROWID.
Exercise
You run EXPLAIN PLAN for WHERE UPPER(email) = '[email protected]' on a table that has only a plain index on email. The plan shows TABLE ACCESS FULL. Give the reason and two ways to fix it.
Show answer
The function UPPER wraps the column, so a plain index on email cannot be used to locate matching rows. The first fix is to create a function-based index on UPPER(email). The second is to add a virtual column defined as UPPER(email) and index that column. The query must use the same expression, or the virtual column.
Exercise
DISPLAY_CURSOR with ALLSTATS LAST shows E-Rows of 12 and A-Rows of 48000 on an INDEX RANGE SCAN. What does this gap suggest, and what is a sensible first action?
Show answer
The optimizer badly underestimated how many rows match, which usually means stale or missing statistics, or a skewed column. A sensible first action is to gather fresh statistics on the table and its indexes, then run the query and compare the plan again.