Sub topic 1
What an execution plan is
Prerequisites
This chapter assumes you know basic SQL (SELECT, joins, WHERE, GROUP BY), understand tables, indexes, and primary keys, and can run SQL in SQL*Plus, SQL Developer, or SQLcl against a schema such as the HR sample schema. You also need the privilege to run EXPLAIN PLAN.
What an execution plan is
An execution plan is the sequence of steps that Oracle's optimizer chooses to answer a SQL statement. The optimizer decides which tables to read, whether to use an index, which join method to apply, and the order of these steps. The same query can have several possible plans, and the chosen plan largely determines how much work the database does.
Generating a plan with EXPLAIN PLAN and DBMS_XPLAN.DISPLAY
EXPLAIN PLAN stores the optimizer's estimated plan in PLANTABLE without running the query. DBMSXPLAN.DISPLAY formats the most recent plan for display.
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);The output contains estimates only. No rows are fetched, so the figures can differ from what the real run produces.
Reading the plan: columns, indentation and execution order
Each plan line has an Id, an Operation, and a Name, which is the table or index the operation touches. The TYPICAL format also shows Rows (estimated row count), Bytes, and Cost, where Cost is the optimizer's relative estimate and not a time.
Indentation shows parent and child relationships. Child operations feed rows to their parent. Execution starts with the most indented operations and works outward toward Id 0 at the top. When two operations share an indent level, the one listed higher runs first.
Choosing a format: BASIC, TYPICAL, ALL and modifiers
The format argument of DISPLAY controls which columns and sections appear.
| Format | What it shows |
|---|---|
| BASIC | Id, Operation, and Name only |
| TYPICAL | Default. Adds Rows, Bytes, Cost, Time, and Predicate Information |
| ALL | Everything in TYPICAL plus Alias and Column Projection sections |
Modifiers add or remove sections from a base format. For example, format => 'BASIC +PREDICATE' shows the minimal layout plus Predicate Information, and -COST removes the cost columns.
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY(format => 'BASIC +PREDICATE'));Exercise
Exercise
You run EXPLAIN PLAN for a query and then call DBMS_XPLAN.DISPLAY with no arguments. Does the query's result set get returned, and which operation in the plan runs first if two child operations appear at the same indent level?
Show answer
No. EXPLAIN PLAN only stores the estimated plan in PLAN_TABLE, so the query is not executed and no rows are returned. When two child operations share an indent level, the one listed higher in the output runs first.