Sub topic 3
Reading an Oracle Execution Plan: Columns, Indentation and Execution Order
Assume you know basic SELECT, joins, WHERE clauses, tables, indexes, and primary keys. You also need access to an Oracle Database schema such as the HR sample schema.
An execution plan is a tree of row-source operations. A row source is a set of rows produced by an operation. Oracle's optimizer chooses these operations before the statement runs.
Generate a plan
Run EXPLAIN PLAN FOR before the query. Oracle stores the estimated plan in PLAN_TABLE. Then use DBMS_XPLAN.DISPLAY to format it.
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);A plan commonly contains columns like these:
| Column | Meaning |
|---|---|
| Id | Identifier for an operation in the plan tree |
| Operation | Work Oracle performs, such as `TABLE ACCESS` or `HASH JOIN` |
| Name | Table, index, or other object used by the operation |
| Rows | Estimated number of rows produced by the operation |
| Bytes | Estimated size of the produced rows |
| Cost | Optimizer cost used to compare plans |
| Time | Estimated elapsed time based on optimizer statistics |
Rows, Bytes, Cost, and Time are estimates in this output. They are not measurements from the query execution.
Read the tree
Indentation shows the relationship between operations. An indented operation is a child of the operation above it. A parent consumes rows produced by its children.
For example, the plan might contain this simplified structure:
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost |
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 4 | 3 |
| 1 | HASH JOIN | | 4 | 3 |
| 2 | TABLE ACCESS FULL | DEPARTMENTS | 27 | 1 |
| 3 | TABLE ACCESS FULL | EMPLOYEES | 4 | 2 |
--------------------------------------------------------------------------------The SELECT STATEMENT is the root. It returns the final result but does little row-processing work itself. The HASH JOIN combines rows from the two table-access operations.
Read the tree from its leaves toward its root:
- Oracle reads rows from the child operations.
- The join consumes those rows.
- The statement returns the join's output.
The plan is a dependency tree, not always a simple one-time top-to-bottom script. With a nested loops join, Oracle may execute the outer child once and repeatedly execute the inner child. A parent can also stop requesting rows before a child has produced all possible rows.
Interpret estimates carefully
The Rows column is often called E-Rows, meaning estimated rows. It describes the number of rows Oracle expects an operation to return. It is not the number of rows actually returned.
The Cost column is an internal comparison value. A cost of 10 is not guaranteed to run twice as fast as a cost of 20. Do not treat cost as elapsed time.
The Id values help identify operations and predicates. They do not alone define execution order. Use indentation and parent-child relationships to understand the data flow.
Use plan output for a specific question
For a quick view, use the BASIC format. For the usual columns and useful details, use TYPICAL. The +PREDICATE modifier adds the predicate section.
EXPLAIN PLAN FOR
SELECT last_name
FROM hr.employees
WHERE department_id = 50;
SELECT *
FROM TABLE(DBMS_XPLAN.DISPLAY(format => 'BASIC +PREDICATE'));Exercise
In a plan, a HASH JOIN is the parent of two indented table-access operations. Which operations produce rows first, and what does the join do with them?
Show answer
The table-access operations produce rows for the join. The HASH JOIN consumes those child row sources and combines matching rows before passing its result to its parent.