Sub topic 14
A step-by-step workflow for reading a plan
This chapter turns the earlier pieces into one repeatable routine. It assumes you can run EXPLAIN PLAN and DBMS_XPLAN.DISPLAY_CURSOR, and that you know the basic access paths (full scan, index range scan) and join methods (nested loops, hash join). It also assumes you know what the Predicate Information section is. The routine has seven steps. Each step answers one question, and you only move on when you have an answer.
Why a fixed order matters
A plan can have dozens of lines, and nearly every line looks plausible by itself. Readers who scan for "TABLE ACCESS FULL" and blame it tend to fix the wrong thing. A full scan of a 27-row table is fine. A nested loop that starts a million times is not, even though both look ordinary.
A fixed order does two things. It makes you look at measured numbers before you form a theory, and it makes you change one thing at a time.
Step 1: Capture the actual plan
Start with the plan that actually ran, not the estimate. EXPLAIN PLAN can differ from the real plan because of bind variables, session settings and adaptive features. It also has no runtime numbers at all.
SET SERVEROUTPUT OFF
SELECT /*+ GATHER_PLAN_STATISTICS */
e.last_name, d.department_name
FROM hr.employees e
JOIN hr.departments d ON d.department_id = e.department_id
WHERE e.job_id = 'SA_REP'
AND e.department_id = 80;
SELECT *
FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(
format => 'ALLSTATS LAST +PREDICATE +NOTE'));LAST restricts the statistics to the most recent execution. Without it you see totals across all executions of the cursor, which hides a single slow run behind many fast ones. If the statement came from another session, look up its sql_id and child number in V$SQL and pass both to DISPLAY_CURSOR.
Step 2: Read the header and the Note
Before the table, check three things.
- The SQL text and
sql_id. Confirm this is the statement you meant to examine, not a similar one. - The
Plan hash value. It identifies the plan shape. If a query was fast last week and is slow now, a different hash value is strong evidence that the plan changed. - The Note section after the table. Lines such as
dynamic statistics used,this is an adaptive planorSQL plan baseline ... usedchange how you interpret everything above. A plan forced by a baseline cannot be improved by changing statistics until you know the baseline exists.
Step 3: Work out the execution order
Plans are printed as trees, but they do not run top to bottom. Use this rule:
- Start at the top operation and go down to its first child (the first line indented one level deeper).
- Keep descending until you reach an operation with no children. That operation runs first.
- Then run its next sibling at the same indentation, if any.
- A parent runs, in the sense of producing its output, after its children have supplied rows.
The Id column is not the execution order. Here is an illustrative output. The numbers are invented to show the technique and are not real HR data.
-----------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | Buffers |
-----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | 30 | 120 |
|* 1 | HASH JOIN | | 1 | 3 | 30 | 120 |
| 2 | TABLE ACCESS FULL| DEPARTMENTS | 1 | 27 | 27 | 8 |
|* 3 | TABLE ACCESS FULL| EMPLOYEES | 1 | 3 | 30 | 112 |
-----------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("D"."DEPARTMENT_ID"="E"."DEPARTMENT_ID")
3 - filter("E"."JOB_ID"='SA_REP' AND "E"."DEPARTMENT_ID"=80)Operation 2 is the first child of the hash join, so it runs first and builds the hash table. Operation 3 runs next and probes it. Operation 1 then joins, and operation 0 returns the rows. The order is 2, 3, 1, 0.
Step 4: Compare E-Rows with A-Rows
This is the most useful step. E-Rows is the optimizer's estimate per execution of the operation. A-Rows is the actual count summed over all executions. Starts is the number of executions, so the fair comparison is E-Rows × Starts against A-Rows.
In the example, operation 2 matches (27 and 27), but operation 3 estimated 3 and returned 30. That is a tenfold underestimate. Work from the leaves upward and find the first operation where the two numbers diverge by an order of magnitude or more. Errors propagate upward, so every operation above that point is built on a wrong number. Fix the first bad estimate and the later ones often correct themselves.
Common causes of a bad estimate:
| Symptom | Likely cause | Typical remedy |
|---|---|---|
| Underestimate on a filter with several predicates | The optimizer assumes the columns are independent, but they are correlated | Column group extended statistics |
| Estimate of 1 row on a large table | Stale or missing statistics | Gather statistics |
| Large error on a skewed column | No histogram, or a bind peeked value that is atypical | Histogram, review bind usage |
| Estimates fine in the leaves, wrong after a join | Join cardinality or join correlation | Review join conditions and statistics on the join columns |
In the example, job_id and department_id are correlated. Most sales representatives sit in one department. Multiplying their selectivities underestimates the result.
Step 5: Find where work accumulates
Buffers counts logical reads and A-Time records elapsed time. Both are cumulative, so a parent includes the cost of its children. To find the operation that did the work, subtract the children's values from the parent's.
In the example, operation 3 read 112 of the 120 buffers, so most of the work happened in the scan of EMPLOYEES. Be careful when Starts is large. A cheap inner operation that runs 100,000 times can dominate the total, even though each execution shows only a few buffers.
Also compare buffers with rows. Reading 112 buffers to return 30 rows is acceptable for a small table. Reading 50,000 buffers to return 30 rows suggests the access path is not selective enough.
Step 6: Check the predicates
Look at the Predicate Information section for every operation that is slow or has a bad estimate.
- An
accesspredicate narrows where the operation looks, such as the start and stop keys of an index range scan, or the join key of a hash join. - A
filterpredicate is applied after the rows have been read.
If you expected an index to answer a condition and the condition appears as filter on the table line, the index is not helping with it. A function on a column (UPPER(last_name) = 'KING') or an implicit data type conversion (INTERNAL_FUNCTION(...)) can stop an index from being used for that condition.
In the example, operation 3 filters on both columns after a full scan. If employees were large, an index on (job_id, department_id) would turn those filters into access predicates. At 107 rows, the full scan is probably the right choice, and the real problem is only the misestimate.
Step 7: Change one thing and re-measure
Pick the single change that addresses the first bad estimate or the largest cost. Then run the statement again and compare the new Plan hash value, A-Rows, Buffers and A-Time against your notes. Changing several things at once leaves you unable to tell which one helped.
If the plan changed but you cannot reproduce the earlier plan, and the statement was captured by AWR, retrieve the old plan with DISPLAY_AWR.
SELECT *
FROM TABLE(DBMS_XPLAN.DISPLAY_AWR(
sql_id => '&sql_id',
format => 'TYPICAL'));Common mistakes in this workflow
- Reading the
Idcolumn as the run order. Use the tree rule from step 3. - Comparing
A-RowstoE-Rowswithout multiplying byStarts. - Treating cumulative
BuffersorA-Timeas belonging to one line. - Tuning from an
EXPLAIN PLANresult when the application runs with bind variables and different session settings. - Concluding that a full table scan is the problem without checking the table size and the
A-Rowsit returns.
Exercise
A plan shows a nested loops join. The outer table access has Starts = 1, E-Rows = 5, A-Rows = 4,000. The inner index range scan has Starts = 4,000, E-Rows = 1, A-Rows = 4,000, and Buffers = 12,000. Which step finds the root problem, what is it, and what would you check next?
Show answer
Step 4 finds it. The outer access estimated 5 rows but returned 4,000, so the first estimate that diverges is on the outer table. Because the optimizer expected only 5 starts of the inner scan, nested loops looked cheap. With 4,000 starts, the inner scan costs 12,000 buffers. Next, check the predicates on the outer table (step 6) for correlated columns or missing statistics, fix the estimate, and re-measure. A hash join may then be chosen.
Exercise
A plan has operations with Ids 0 to 4. Id 1 is a HASH JOIN. Ids 2 and 4 are its children, and Id 3 is a child of Id 2. In which order do the operations run?
Show answer
The order is 3, 2, 4, 1, 0. Descend from Id 1 to its first child Id 2, then to Id 2's child Id 3, which has no children and runs first. Then Id 2 completes, then its sibling Id 4 runs, then the hash join Id 1, and finally Id 0 returns the rows.