Sub topic 12
Spotting Where Estimates and Actuals Diverge
This chapter assumes that you can read basic SELECT statements, joins, WHERE clauses, tables, and indexes. You also need access to an Oracle database and permission to run DBMS_XPLAN.DISPLAY_CURSOR.
The optimizer chooses a plan using estimated row counts. After the statement runs, Oracle can show actual row counts. Comparing these values helps you find the operation where the optimizer's assumptions stopped matching the data.
The important runtime columns
Use GATHER_PLAN_STATISTICS while investigating a statement:
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.salary > 10000;
SELECT *
FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(
format => 'ALLSTATS LAST'));The ALLSTATS LAST format displays statistics for the most recent execution of the statement in the current session.
| Column | Meaning |
|---|---|
| E-Rows | Rows the optimizer estimated for one execution of an operation |
| A-Rows | Rows the operation actually produced during the last execution |
| Starts | Number of times the operation started |
| Buffers | Logical reads used by the operation and its children |
| A-Time | Time spent in the operation |
E-Rows is an estimate made before execution. A-Rows is evidence collected during execution. A large difference between them is called a cardinality misestimate. Cardinality means the number of rows produced by an operation.
Find the first divergence
Read the plan from its child operations toward its parent operations. For each operation, compare E-Rows with A-Rows.
The first operation with a large mismatch is often the most useful place to investigate. Later estimates may also be wrong because they depend on the incorrect row count from an earlier operation.
For example, if an index range scan estimates 100 rows but returns 10,000, the optimizer may choose nested loops because it expects a small input. The large actual input can make that join method expensive. The join is a symptom, but the inaccurate scan estimate is the likely starting point.
Do not compare the numbers mechanically when Starts is greater than one. An operation called repeatedly may return a small number of rows per start but many rows in total. Consider Starts, the operation's parent, and the plan's execution structure.
Use buffers to measure the cost
A row-count mismatch does not always mean the statement is slow. Use Buffers to see how much logical I/O the operation caused. A plan that returns few rows but reads many buffers may be doing unnecessary work. A nested-loops inner operation with a high Starts value can repeatedly visit the same table or index.
A-Time can identify an expensive operation, but timing depends on cache state, concurrent activity, and the execution environment. Treat it as useful evidence rather than a permanent property of the plan.
Common causes of divergence include stale or missing statistics, data skew, correlated predicates, expressions on columns, and bind values that select very different portions of a table. Check the earliest large mismatch before changing indexes or hints.
Exercise
An operation has E-Rows = 20, A-Rows = 20,000, Starts = 1, and Buffers = 25,000. What should you investigate first?
Show answer
Investigate that operation's cardinality estimate and the table or predicates producing its rows. The 1,000-to-1 mismatch can cause poor choices for operations above it, and the high buffer count shows substantial logical I/O.