Sub topic 1
What an Execution Plan Is
An execution plan is the set of operations Oracle chooses to run a SQL statement. It describes how Oracle will find rows, join tables, apply conditions, and produce the result. The plan is a decision made by the optimizer, Oracle's component that compares possible strategies and estimates their cost.
This chapter assumes that you know basic SELECT statements, joins, and WHERE clauses. You should also know what tables, indexes, and primary keys are. You need access to an Oracle Database schema, such as the HR sample schema, through SQL*Plus, SQL Developer, or SQLcl.
Prerequisites
You should be comfortable reading a query such as this one:
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;The query asks for employees with a salary above 10,000 and the name of each employee's department. Oracle must decide how to read employees, how to read departments, how to match the rows, and when to apply the salary condition.
What the Optimizer Decides
For each statement, the optimizer considers choices such as:
- Read every row in a table, called a full table scan.
- Use an index to find a smaller set of rows.
- Read one table first and look up matching rows in another table.
- Read both tables and build a hash structure to join them.
- Sort rows before joining or returning them.
The optimizer estimates the number of rows produced by each operation. It also estimates work such as disk reads, memory use, and CPU time. It then selects a plan with the lowest estimated cost according to its model.
The cost is not a time measurement in seconds. A plan with a lower cost is not guaranteed to finish faster. The optimizer can make incorrect estimates when statistics are old, data is unevenly distributed, or a condition is difficult to model.
A Plan Is a Tree
Oracle represents a plan as a tree of operations. An operation near the bottom usually supplies rows to an operation above it. The top operation produces the statement's final result.
Indentation in a text plan shows the tree structure. It does not mean that Oracle executes every line from top to bottom. Oracle normally starts with the deepest child operation, produces rows, and passes them to its parent. The exact flow depends on the operation and its inputs.
Estimated and Actual Plans
EXPLAIN PLAN asks Oracle to generate an estimated plan without executing the query. DBMS_XPLAN.DISPLAY formats that plan for reading:
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 displayed plan is useful for learning what Oracle expects to happen. It is not proof of what happened during execution. Bind values, session settings, adaptive behavior, or a different cursor can cause the executed statement to use another plan. Later, DBMS_XPLAN.DISPLAY_CURSOR can show the plan stored for an executed cursor and, with statistics enabled, compare estimates with observed results.
Exercise
Exercise
Why can a plan with a lower estimated cost still run slower than another plan?
Show answer
The optimizer's estimates can be wrong because of stale statistics, uneven data, or conditions that are difficult to estimate. Cost is an estimate, not elapsed time.