Sub topic 3
Reading the plan: columns, indentation and execution order
This chapter assumes you can run EXPLAIN PLAN and DBMS_XPLAN.DISPLAY from the previous chapter, and that you know what a join, an index and a primary key are. Here you will learn what each column of a plan means and how the indentation encodes a tree. You will also learn the rule for the order in which Oracle runs the operations.
A plan is a tree printed as a table
A plan is a tree of row sources. A row source is an operation that produces rows, such as a table scan or a join. Each operation hands its rows to its parent. The parent combines or filters them and passes them further up. The root of the tree is the final result that goes back to the client.
DBMS_XPLAN prints this tree as a table. Here is a typical TYPICAL-format plan for a join of EMPLOYEES and DEPARTMENTS. The numbers are illustrative, and yours will differ with your data and statistics.
Plan hash value: 1343509718
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 15 | 480 | 6 (17)| 00:00:01 |
|* 1 | HASH JOIN | | 15 | 480 | 6 (17)| 00:00:01 |
| 2 | TABLE ACCESS FULL| DEPARTMENTS | 27 | 432 | 3 (0)| 00:00:01 |
|* 3 | TABLE ACCESS FULL| EMPLOYEES | 15 | 240 | 3 (0)| 00:00:01 |
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("D"."DEPARTMENT_ID"="E"."DEPARTMENT_ID")
3 - filter("E"."SALARY">10000)The columns
| Column | Meaning |
|---|---|
| Id | The position of the operation in the plan. Ids are assigned top to bottom. The asterisk next to an Id means that operation has an entry under Predicate Information. |
| Operation | What the step does. Its indentation shows its place in the tree. |
| Name | The object the operation touches, such as a table or index. It is blank for operations like joins. |
| Rows | The optimizer's estimate (E-Rows) of how many rows this operation returns each time it runs. |
| Bytes | The estimated size of those rows. |
| Cost (%CPU) | The optimizer's internal cost estimate for the operation and everything below it. The percentage is the CPU share of that cost. |
| Time | An estimated elapsed time, derived from the cost. |
Some of these columns are easy to misread.
- Cost is cumulative. The cost of a parent includes the cost of its children. In the plan above, the join at Id 1 has cost 6 and its two scans cost 3 each.
- Cost is not a time or a unit you can compare across statements. It is only meaningful for comparing alternative plans for the same statement, under the same statistics and settings.
- Rows is an estimate. Nothing has run yet in an
EXPLAIN PLANplan. Later chapters compare it with actual rows. - Rows is per execution of the operation. If an operation runs many times, as the inner side of a nested loop does, the Rows value describes one run.
- Time is derived from Cost. Do not treat it as a prediction of how long the statement will take.
Indentation shows parent and child
Operations are indented one level under their parent. In the plan above, Id 1 (HASH JOIN) is indented under Id 0, and Ids 2 and 3 are indented under Id 1. So Ids 2 and 3 are siblings, and both are children of the join.
The plan table stores the same tree explicitly. PLAN_TABLE has an ID column and a PARENT_ID column. You can rebuild the tree yourself with a hierarchical query. Tagging the plan with a STATEMENT_ID keeps it separate from other plans stored in the same table.
EXPLAIN PLAN SET STATEMENT_ID = 'ch3' 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 id, parent_id,
LPAD(' ', 2 * (LEVEL - 1)) || operation || ' ' || options AS operation,
object_name
FROM plan_table
START WITH id = 0 AND statement_id = 'ch3'
CONNECT BY PRIOR id = parent_id AND statement_id = 'ch3'
ORDER SIBLINGS BY id;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY('PLAN_TABLE', 'ch3', 'BASIC'));The hierarchical query shows that the indentation is only a view of the PARENT_ID links. The DISPLAY call at the end prints the same plan in the usual layout.
The order of execution
The Id column does not tell you the order in which operations run. Id 0 is the root, and it finishes last. Use this rule to find the order:
- Start at the root and go down to its first child. Keep going down through first children until you reach an operation with no children. That operation runs first.
- When an operation finishes, move to its next sibling below it at the same indentation. If that sibling has children, descend to its first leaf, as in step 1.
- When all the children of a parent have run, the parent runs.
The rule has one important exception. For a nested loops join, the second child does not run once. It runs again for each row returned by the first child, so it is interleaved with the first child rather than run after it. A hash join runs differently. Its first child is the build input, which Oracle reads fully into memory. Its second child is the probe input, whose rows are matched against the in-memory data.
For the hash join plan above, the order is Id 2, Id 3, Id 1, Id 0. Oracle reads DEPARTMENTS to build the hash table. It then scans EMPLOYEES, filters on salary and probes the hash table, and finally returns the joined rows.
A deeper example: nested loops with indexes
Consider a query that picks a few employees by department and looks up each department. Here is a plan the optimizer could produce for it. Again, the numbers are illustrative.
-----------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)|
-----------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 45 | 7 (0)|
| 1 | NESTED LOOPS | | 45 | 7 (0)|
| 2 | TABLE ACCESS BY INDEX ROWID BATCHED | EMPLOYEES | 45 | 2 (0)|
|* 3 | INDEX RANGE SCAN | EMP_DEPARTMENT_IX | 45 | 1 (0)|
| 4 | TABLE ACCESS BY INDEX ROWID | DEPARTMENTS | 1 | 1 (0)|
|* 5 | INDEX UNIQUE SCAN | DEPT_ID_PK | 1 | 0 (0)|
-----------------------------------------------------------------------------------------------The tree looks like this.
Apply the rule. Going down first children from the root leads to Id 3, the index range scan, so it runs first. It returns rowids, and Id 2 uses them to fetch employee rows. For each of those rows, Oracle probes the department side: Id 5 finds the department's rowid, then Id 4 fetches the row. Id 1 combines the pairs and Id 0 returns them. The order is 3, 2, 5, 4, 1, 0.
Ids 4 and 5 run once per employee row. Their Rows value of 1 is the estimate for each run, not the total. The Starts column, introduced in the runtime statistics chapter, shows how many times each step really ran.
Common mistakes when reading the tree
- Reading in Id order. Id 2 comes before Id 3 in the list, but Id 3 runs first in the nested loops example.
- Assuming the most-indented line is the whole story. Deep indentation means the operation is far from the root. It does not mean it is slow or important.
- Comparing Cost across different statements. Cost is an estimate for one plan, not a measurement.
- Ignoring the asterisks. An Id marked with
*has a predicate that changes what the operation does. The*on Id 3 above means the index range scan has an access predicate. Reading those predicates is the subject of a later chapter. - Treating the Rows of an inner step as a total. Multiply by the number of starts to get the total.
Exercise
A plan has these operations, indented as shown: Id 0 SELECT STATEMENT; Id 1 HASH JOIN (child of 0); Id 2 TABLE ACCESS FULL on ORDERS (child of 1); Id 3 HASH JOIN (child of 1); Id 4 TABLE ACCESS FULL on CUSTOMERS (child of 3); Id 5 TABLE ACCESS FULL on REGIONS (child of 3). In what order do the operations execute?
Show answer
Start at the root and descend through first children: Id 0, Id 1, Id 2. Id 2 has no children, so it runs first and builds the hash table for Id 1. Its sibling Id 3 comes next, but Id 3 has children, so descend to Id 4 and run it, then Id 5. Then Id 3 runs. After that Id 1 runs, and finally Id 0. The order is 2, 4, 5, 3, 1, 0.
Exercise
In the nested loops example, the Rows value of Id 4 is 1 while Id 1 estimates 45 rows. Why do these not contradict each other?
Show answer
Rows is the estimate for one execution of the operation. Id 4 runs once for each of the roughly 45 employee rows, and each run is expected to return one department row. The join at Id 1 therefore returns about 45 rows in total.