Sub topic 2
Generating a Plan with EXPLAIN PLAN and DBMS_XPLAN.DISPLAY
This chapter assumes that you know basic SELECT statements, joins, WHERE clauses, tables, indexes, and primary keys. You also need access to an Oracle database, such as the HR sample schema, through SQL*Plus, SQL Developer, or SQLcl.
What `EXPLAIN PLAN` Does
An execution plan is the set of operations Oracle expects to use for a SQL statement. The optimizer creates this plan by considering table statistics, indexes, join conditions, and filters.
EXPLAIN PLAN asks Oracle to create an estimated plan without running the query. Oracle stores the plan in a table named PLAN_TABLE. DBMS_XPLAN.DISPLAY then formats that stored plan so you can read 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);The first statement creates the plan. The second statement displays it. The query itself does not run, so the output contains estimates rather than measured execution results.
The Basic Workflow
Use this sequence when you want to inspect a statement:
- Write the exact SQL statement you want to study.
- Run
EXPLAIN PLAN FORimmediately before that statement. - Query
DBMS_XPLAN.DISPLAY. - Read the displayed operations, estimates, and predicates.
- Test important conclusions with an actual cursor plan.
The relationship between the statements is:
EXPLAIN PLAN FOR replaces the previous plan in the relevant PLAN_TABLE context. Display the plan before explaining another statement if you need to preserve or inspect the result.
Choosing the Display Format
The format argument controls how much information Oracle prints.
| Format | Typical use |
|---|---|
| `BASIC` | Show operation names and object names with minimal details |
| `TYPICAL` | Show the standard plan, estimates, and useful predicates |
| `ALL` | Show extensive plan details and additional sections |
TYPICAL is a useful starting point. Use BASIC when you need a compact overview. Add modifiers when you need a specific section.
EXPLAIN PLAN FOR
SELECT last_name
FROM hr.employees
WHERE department_id = 50;
SELECT *
FROM TABLE(
DBMS_XPLAN.DISPLAY(
format => 'BASIC +PREDICATE'
)
);The +PREDICATE modifier requests the Predicate Information section. This section identifies conditions used to access rows and conditions applied after rows are found. You will use it when interpreting indexes and filters.
You can also request outline information, which shows optimizer hints that can reproduce the displayed plan:
SELECT *
FROM TABLE(
DBMS_XPLAN.DISPLAY(
format => 'TYPICAL +OUTLINE'
)
);Common Practical Errors
Run EXPLAIN PLAN FOR directly before the target statement. A tool-generated statement, such as a metadata query, can otherwise become the statement whose plan you display.
Do not confuse a displayed estimated plan with an actual execution plan. Different bind values, session settings, statistics, or adaptive behavior can cause the executed cursor to use another plan. For actual behavior, run the statement and inspect it with DBMS_XPLAN.DISPLAY_CURSOR.
Exercise
Does EXPLAIN PLAN execute the query and return its rows?
Show answer
No. It creates an estimated plan and stores it in PLANTABLE. DBMSXPLAN.DISPLAY formats that stored plan. To inspect measured execution statistics, use DBMSXPLAN.DISPLAYCURSOR after running the statement.