Sub topic 12
Spotting where estimates and actuals diverge
This chapter assumes you can already produce an actual plan with DBMS_XPLAN.DISPLAY_CURSOR and the GATHER_PLAN_STATISTICS hint, and that you know the Starts, E-Rows, A-Rows, Buffers and A-Time columns by name. Here you will learn to compare them to find the operation where the optimizer's picture of your data first goes wrong.
Why the comparison matters
The optimizer picks a plan from cost estimates, and those estimates come from cardinality: the number of rows each operation is expected to return. If a cardinality estimate is badly wrong, the costs built on it are wrong too. The optimizer may then choose a nested loops join where a hash join would be far faster, or an index range scan where a full scan would be better.
The ALLSTATS LAST format lets you check the estimates against reality. E-Rows is the estimate. A-Rows is what the operation actually produced during the last execution. A large gap between them tells you the optimizer believed something false about your data.
A reproducible example
Correlated columns are a classic cause of misestimates. The optimizer assumes predicates on different columns are independent, so it multiplies their selectivities. This script creates two columns that always hold the same value:
CREATE TABLE corr_demo AS
SELECT level AS id,
MOD(level, 10) AS a,
MOD(level, 10) AS b,
RPAD('x', 50, 'x') AS pad
FROM dual
CONNECT BY level <= 100000;
BEGIN
DBMS_STATS.GATHER_TABLE_STATS(ownname => USER, tabname => 'CORR_DEMO');
END;
/
SELECT /*+ GATHER_PLAN_STATISTICS */ COUNT(*)
FROM corr_demo
WHERE a = 1 AND b = 1;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(format => 'ALLSTATS LAST'));Each column has 10 distinct values, so each predicate has a selectivity of 1/10. Assuming independence, the optimizer estimates 100,000 × 1/10 × 1/10 = 1,000 rows. In fact every row with a = 1 also has b = 1, so 10,000 rows match. The output looks like this. Your timings and buffer counts will differ.
------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers |
------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | 1 |00:00:00.01 | 1500 |
| 1 | SORT AGGREGATE | | 1 | 1 | 1 |00:00:00.01 | 1500 |
|* 2 | TABLE ACCESS FULL| CORR_DEMO | 1 | 1000 | 10000 |00:00:00.01 | 1500 |
------------------------------------------------------------------------------------Operation 2 estimated 1,000 rows and returned 10,000, an underestimate by a factor of 10. Operation 1 is correct only because COUNT(*) always returns one row. A correct row count above a wrong one does not mean the wrong one is harmless.
Reading the columns correctly
E-Rows is per start, A-Rows is cumulative
This is the most common mistake. E-Rows is the estimate for a single execution of the operation. A-Rows is the total produced across all starts. When Starts is greater than 1, compare A-Rows / Starts with E-Rows.
| Id | Operation | Name | Starts | E-Rows | A-Rows |
| 2 | NESTED LOOPS | | 1 | 30 | 15000 |
| 3 | TABLE ACCESS FULL | ORDERS | 1 | 10 | 5000 |
|* 4 | TABLE ACCESS BY INDEX ROWID| ORDER_ITEMS | 5000 | 3 | 15000 |For operation 4, the 15,000 actual rows over 5,000 starts is 3 rows per start, which matches the estimate of 3. This operation is fine. The real error is operation 3, which was expected to return 10 rows and returned 5,000. That error multiplied the number of starts of everything below it.
Buffers and A-Time are cumulative too
Buffers and A-Time include the work of all child operations. To find the cost of one operation, subtract its children's values from its own. A high Buffers figure on a join does not mean the join is the problem if one child accounts for almost all of it.
Finding the first divergence
Errors propagate upward. A wrong estimate low in the tree makes every estimate above it wrong, so the operation closest to the leaves is the usual culprit. Use this order:
- Start at the deepest, earliest-executed operations (the leaf operations, see the execution-order chapter).
- For each, compute the actual per-start rows (
A-Rows / Starts) and compare it withE-Rows. - Treat a ratio of about 10 or more in either direction as significant. Small differences are normal because estimates come from statistics, not from counting.
- Go up the tree and find the first operation whose estimate is wrong even though its inputs were estimated correctly. That operation, usually a join or a filter, is where the optimizer's model fails.
Common causes
| Symptom in the plan | Likely cause | Typical fix |
|---|---|---|
| Filter on one column is off by a large factor | Skewed data with no histogram, or stale statistics | Gather statistics, create a histogram |
| Several predicates on one table underestimate | Correlated columns | Extended (column group) statistics |
| Estimate is a default guess such as 1 row | Missing statistics, or an expression on the column | Gather statistics, or create expression statistics |
| Predicate uses a function on a column, such as `UPPER(name)` | The optimizer cannot use the column statistics | Extended expression statistics, or rewrite the predicate |
| Join estimate is wrong while both inputs are right | Join columns are correlated, or have skew | Histograms, extended statistics, or dynamic sampling |
| Estimate is right for one bind value and wrong for another | Bind peeking on skewed data | Review the bind values with `+PEEKED_BINDS` |
You can confirm the cause by looking at the Predicate Information section for the diverging operation. It shows exactly which conditions the estimate had to account for.
Fixing the example
For the correlated columns, create a column group so the optimizer has statistics for the pair (a, b). Then re-gather statistics with no_invalidate => FALSE. That makes the cursor stop being reused, so the next execution is parsed again.
SELECT DBMS_STATS.CREATE_EXTENDED_STATS(USER, 'CORR_DEMO', '(A,B)')
FROM dual;
BEGIN
DBMS_STATS.GATHER_TABLE_STATS(
ownname => USER,
tabname => 'CORR_DEMO',
no_invalidate => FALSE);
END;
/
SELECT /*+ GATHER_PLAN_STATISTICS */ COUNT(*)
FROM corr_demo
WHERE a = 1 AND b = 1;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(format => 'ALLSTATS LAST'));The column group has 10 distinct values, so the estimate is now 100,000 / 10 = 10,000 rows, which matches A-Rows. The same query now runs with a plan the optimizer chose for the correct row count.
Cases where a gap does not mean a problem
- Execution stopped early. If a parent stops asking for rows, the child's
A-Rowsis lower than itsE-Rows. This happens withROWNUM,FETCH FIRST,EXISTSsubqueries, and clients that fetch only the first rows. The estimate is for the full result. Compare estimates only when the operation ran to completion. - Starts is 0. An operation that never ran shows
Startsof 0 and noA-Rows. Branches skipped by aFILTERoperation or a failed join can look like this. - Small numbers. An estimate of 1 against an actual of 5 is a factor of 5 but usually irrelevant. Gaps matter when they change the plan or the work done, which
Buffersshows. - Adaptive features. On later executions Oracle may add a
Noteabout statistics feedback or SQL plan directives. If the second run is correct and the first was not, the optimizer corrected itself. Do not assume your own fix did it.
Summary checklist
- Compare
E-RowswithA-Rows / Starts, never with rawA-RowswhenStartsis above 1. - Look for the deepest operation with a gap of about 10 times or more.
- Treat
BuffersandA-Timeas cumulative. - Check the operation's predicates to name the cause, then fix the statistics.
- Re-run with the hint and verify that the estimate and the actual agree.
Exercise
A plan shows an index access on the inner side of a nested loops join with Starts = 2000, E-Rows = 1 and A-Rows = 2000. Is this operation misestimated? What else would you check?
Show answer
No. A-Rows / Starts is 2000 / 2000 = 1 row per start, which equals E-Rows. The operation is correct. Check the outer (driving) side instead: its E-Rows tells you how many starts the optimizer expected, so if it expected far fewer than 2000 rows, the outer operation is the misestimate.
Exercise
A query joins two tables, and the plan has E-Rows = 50 and A-Rows = 40000 on the hash join. Both table accesses beneath it have E-Rows and A-Rows within 10% of each other. Where does the divergence first appear, and name two possible causes.
Show answer
The first divergence is at the hash join itself, because its inputs are estimated correctly. Possible causes are skew or correlation in the join columns, so the optimizer's join selectivity is wrong, or a join condition that involves an expression the statistics do not cover. Fixes include a histogram or extended statistics on the join columns, or dynamic sampling.