Sub topic 2
Checking index use in an execution plan
An execution plan is the list of steps Oracle takes to run a statement. Reading it is the only reliable way to know whether your index is used. This chapter shows how to produce a plan, how to read the access path, and how to compare what the optimizer expected with what happened.
It assumes you can run SQL in SQL*Plus or SQL Developer and know what a B-tree index is. Later chapters rely on the plan-reading skills taught here.
Two ways to get a plan
Oracle can show you the plan it would use, or the plan it did use.
- Estimated plan:
EXPLAIN PLANstores the plan in a plan table without running the statement.DBMS_XPLAN.DISPLAYformats it. - Actual plan:
DBMS_XPLAN.DISPLAY_CURSORformats the plan of a statement that already ran and is still in the shared pool. It can also show real row counts.
EXPLAIN PLAN and DBMS_XPLAN.DISPLAY
EXPLAIN PLAN FOR
SELECT order_id, order_date
FROM orders
WHERE customer_id = 42;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);EXPLAIN PLAN writes into PLAN_TABLE. DISPLAY reads the most recent plan you stored. The default TYPICAL format includes a Predicate Information section. If you want the predicates explicitly, ask for them:
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY(format => 'BASIC +PREDICATE'));A plan for the query above, with an index on orders (customer_id), looks like this. The numbers are illustrative.
---------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
---------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 12 | 192 | 4 (0)|
| 1 | TABLE ACCESS BY INDEX ROWID BATCHED| ORDERS | 12 | 192 | 4 (0)|
|* 2 | INDEX RANGE SCAN | ORDERS_CUST_IX| 12 | | 1 (0)|
---------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("CUSTOMER_ID"=42)Actual plan with DISPLAY_CURSOR
Run the statement first, then ask for its plan. The GATHER_PLAN_STATISTICS hint makes Oracle count the rows each step really returns.
SELECT /*+ GATHER_PLAN_STATISTICS */ order_id, order_date
FROM orders
WHERE customer_id = 42;
SELECT *
FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(format => 'ALLSTATS LAST'));Called with no sql_id, DISPLAY_CURSOR shows the last statement your session ran. In SQLPlus, run SET SERVEROUTPUT OFF first. Otherwise the last statement is the hidden call that SQLPlus makes to fetch DBMSOUTPUT, and you see the wrong plan. Your user needs SELECT access to V$SESSION, V$SQL and `V$SQLPLAN`, or a role that grants them.
Reading the operations
Read the plan from the innermost, most indented step outward. The Id column numbers the steps, and the Operation and Name columns tell you what each step does and which object it touches.
| Operation | Meaning |
|---|---|
| `TABLE ACCESS FULL` | Reads every block of the table up to the high-water mark. |
| `INDEX UNIQUE SCAN` | Finds at most one entry through a unique index. |
| `INDEX RANGE SCAN` | Descends the tree, then reads a contiguous range of entries. |
| `INDEX FULL SCAN` | Reads all leaf entries in index order. |
| `INDEX FAST FULL SCAN` | Reads all leaf blocks with multiblock reads, in no guaranteed order. |
| `INDEX SKIP SCAN` | Probes a composite index without its leading column (a later chapter covers it). |
| `TABLE ACCESS BY INDEX ROWID` | Fetches table rows using the rowids the index produced. |
An index operation alone does not prove the index helps. An INDEX FULL SCAN reads the entire index, which may cost more than it saves. You need the predicate section to judge it.
Access and filter predicates
The Predicate Information section lists conditions per step and labels each one.
- access: the condition is used to navigate the index or table, so it limits how much is read.
- filter: the condition is checked on rows after they were read, so it limits only what is returned.
A star next to the Id marks a step that has a predicate. Compare a non-sargable query with its rewrite:
EXPLAIN PLAN FOR
SELECT order_id
FROM orders
WHERE TRUNC(order_date) = DATE '2026-01-15';
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY(format => 'BASIC +PREDICATE'));-------------------------------------------
| Id | Operation | Name |
-------------------------------------------
| 0 | SELECT STATEMENT | |
|* 1 | TABLE ACCESS FULL| ORDERS |
-------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(TRUNC(INTERNAL_FUNCTION("ORDER_DATE"))=TO_DATE(' 2026-01-15 00:00:00',
'syyyy-mm-dd hh24:mi:ss'))The plan has a full table scan and a filter that wraps the column in a function. That wrapped column is the sign that an index on order_date cannot be used. A similar signature appears for implicit conversions:
1 - filter(TO_NUMBER("PHONE_NUMBER")=5551234)TO_NUMBER("PHONE_NUMBER") tells you Oracle converted the column rather than the literal. Later chapters fix both problems. For now, learn to spot them.
Estimated rows versus actual rows
With ALLSTATS LAST, the plan gains these columns:
| Column | Meaning |
|---|---|
| `E-Rows` | Rows the optimizer estimated for the step. |
| `A-Rows` | Rows the step actually produced. |
| `A-Time` | Elapsed time for the step. |
| `Buffers` | Logical reads (blocks read from the buffer cache) for the step. |
A large gap between E-Rows and A-Rows means the optimizer's statistics or assumptions are wrong. It may then pick a poor access path. A step with a small A-Rows but a large Buffers count shows that Oracle visited many blocks to find few rows. That usually means a filter predicate or a poorly chosen index.
Pitfalls when reading plans
- EXPLAIN PLAN does not run the statement. It can differ from the plan used at run time. It treats bind variables as unknown values and does not peek at their values. Prefer
DISPLAY_CURSORfor the plan that actually ran. - A bind variable can change the type.
EXPLAIN PLANmay treat a bind asVARCHAR2, hiding a conversion that occurs with a real numeric bind. - Cost is not time. The
Costcolumn is the optimizer's internal estimate. Use it to compare plans for the same statement, not to compare different statements. - Plans change. Gathering statistics, adding an index, or a new Oracle version can change a plan. Re-check after any such change.
- Index in the plan is not index helps. Check for
accesson the index step and a modestBufferscount.
A repeatable checklist
- Run the statement with
GATHER_PLAN_STATISTICS, then callDISPLAY_CURSORwithALLSTATS LAST. - Find the step that reads the table you care about. Is it
TABLE ACCESS FULLor an index operation? - For an index step, check whether the predicate is
accessorfilter. - Compare
E-RowswithA-Rows. Note any large gap. - Check
Buffersagainst the number of rows returned. - Change one thing (a predicate, an index, or statistics) and read the plan again.
Exercise
A plan shows INDEX RANGE SCAN on step 2 with filter("STATUS"='OPEN') and no access predicate, and step 1 shows 40,000 Buffers for 5 returned rows. Does the index serve this query well? What would you check next?
Show answer
No. With no access predicate, the index is read widely and the condition only discards rows afterwards, which explains the high Buffers count for so few rows. Check whether the predicate is wrapped in a function or conversion, and whether the index columns match the WHERE clause, particularly the leading column.
Exercise
Why can EXPLAIN PLAN show a different plan from the one a query used when it ran, and which function shows the plan that was actually used?
Show answer
EXPLAIN PLAN does not execute the statement and does not peek at bind variable values, so the optimizer may decide differently at run time. DBMS_XPLAN.DISPLAY_CURSOR shows the plan from the shared pool for the statement that ran.