Sub topic 14
A Step-by-Step Workflow for Reading an Oracle Execution Plan
Assume you know basic SELECT statements, joins, WHERE clauses, tables, indexes, and primary keys. You also need access to an Oracle schema such as HR and permission to use DBMS_XPLAN. The workflow below compares what Oracle expected with what happened during execution.
1. Start with the statement and its question
Before reading the plan, state what the query must do. For example:
- Find employees earning more than 10,000.
- Join each employee to a department.
- Return the employee and department names.
This gives you a way to judge whether an access path or join method makes sense. Do not begin by searching for an index. First understand the rows the query needs.
2. Generate an estimated plan
Use EXPLAIN PLAN when you want to see the optimizer's estimated operations without running the query.
EXPLAIN PLAN FOR
SELECT e.last_name, d.department_name
FROM hr.employees e
JOIN hr.departments d
ON d.department_id = e.department_id
WHERE e.salary > 10000;
SELECT *
FROM TABLE(DBMS_XPLAN.DISPLAY(format => 'TYPICAL'));DBMS_XPLAN.DISPLAY formats the plan stored in PLAN_TABLE. Read the output from the bottom upward. The most indented operations are usually performed first, and their rows flow into the parent operation.
3. Read the tree from the leaves upward
Treat the plan as a data-flow tree:
department access ─┐
├─ join ── result
employee access ───┘A parent operation consumes rows produced by its child operations. For each operation, ask:
- Which object does it read?
- How many rows does Oracle expect (
E-Rows)? - Which operation consumes those rows?
- Is the operation doing more work than the query requires?
The plan is a logical execution order, not a list to read from top to bottom. A TABLE ACCESS operation may run before a join even when the table appears later in the SQL text.
4. Identify access paths and joins
Common access paths include full table scans and index scans. A full table scan reads many or all table blocks. An index range scan reads selected index entries and may then visit table rows by row ID.
Common join methods have different typical uses:
| Join method | Typical situation | Question to ask |
|---|---|---|
| Nested loops | A small row source probes another source repeatedly | Is the inner lookup cheap for every outer row? |
| Hash join | Two larger row sources are joined on compatible columns | Are the estimated row counts reasonable? |
| Sort merge join | Inputs can be sorted and merged | Is the sorting cost justified? |
These are not automatic recommendations. A nested loop can be excellent for a small result and expensive when the outer source is much larger than expected.
5. Inspect predicates
Request predicate details when you need to understand how rows are restricted.
EXPLAIN PLAN FOR
SELECT last_name
FROM hr.employees
WHERE department_id = 50;
SELECT *
FROM TABLE(DBMS_XPLAN.DISPLAY(format => 'BASIC +PREDICATE'));An access predicate helps an operation find rows, often through an index or join key. A filter predicate is applied after rows have been found. A condition appearing only as a filter may mean Oracle reads many rows and discards most of them.
6. Replace estimates with runtime evidence
EXPLAIN PLAN can differ from the cursor that actually ran. To inspect runtime behavior, collect row-source statistics and display the last cursor in the session.
SELECT /*+ GATHER_PLAN_STATISTICS */
e.last_name, d.department_name
FROM hr.employees e
JOIN hr.departments d
ON d.department_id = e.department_id
WHERE e.salary > 10000;
SELECT *
FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(format => 'ALLSTATS LAST'));Compare E-Rows with A-Rows. A large difference identifies a cardinality misestimate. Then inspect Buffers and A-Time. High buffer use or elapsed time near that operation shows where the query spent work.
7. Record a conclusion
End each plan review with a specific finding:
- The estimate was close, but the inner lookup ran too many times.
- The salary predicate filtered most rows after a full scan.
- Actual rows were much higher than estimated at the join input.
- The plan changed, and the new plan used more buffers.
This keeps tuning focused on evidence rather than on changing indexes or hints by guesswork.
Exercise
In what order should you inspect a plan, and what comparison usually reveals a cardinality problem?
Show answer
Follow the tree from its leaf operations toward the root, then compare E-Rows with A-Rows in the runtime plan. A large difference points to an inaccurate estimate.