Sub topic 8
Reading the Note section
This chapter assumes you can already generate a plan with EXPLAIN PLAN and DBMS_XPLAN.DISPLAY, and that you know the plan table columns and the Predicate Information section. It covers the last block of a plan, the Note section. It is short, but it often explains why a plan looks the way it does.
What the Note section is
The Note section comes after the plan table and the Predicate Information. It lists facts about how the optimizer produced the plan, or about how the plan is being executed. It does not describe a row source. Each line starts with a hyphen:
Note
-----
- dynamic statistics used: dynamic sampling (level=2)A note is not an error. Most notes tell you that something outside the SQL text influenced the plan. Examples are missing statistics, a stored baseline, a profile, or a parallel degree set on a table. Two sessions can run identical SQL and get different plans because of exactly these influences. The Note section is where that difference is usually visible.
Notes are also a reproducibility check. If you copy a plan into a ticket and a colleague cannot reproduce it, compare the Note sections first.
When the Note section is printed
The format argument decides whether you see it.
| Format | Note section shown? |
|---|---|
| `BASIC` | No |
| `TYPICAL` (the default) | Yes |
| `ALL` | Yes |
| `ALLSTATS LAST` | Yes, when there is something to report |
| `BASIC +NOTE` | Yes |
| `TYPICAL -NOTE` | No |
If you read plans with BASIC, you will never see a note. This is a common reason people miss that dynamic sampling or a SQL plan baseline was involved. Add the modifier:
EXPLAIN PLAN FOR
SELECT last_name FROM hr.employees WHERE department_id = 50;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY(format => 'BASIC +PREDICATE +NOTE'));If a statement has nothing to report, the section is omitted even when you ask for it. A missing Note section means no note was generated.
Worked example: dynamic sampling
Dynamic sampling is the note you will see most often. When a table has no optimizer statistics, the optimizer reads a small sample of blocks at parse time to estimate row counts. The next example creates a table that has no statistics, because conventional inserts do not gather them.
CREATE TABLE note_demo (
id NUMBER,
grp NUMBER,
pad VARCHAR2(50)
);
INSERT INTO note_demo
SELECT level, MOD(level, 10), RPAD('x', 50, 'x')
FROM dual
CONNECT BY level <= 20000;
COMMIT;
EXPLAIN PLAN FOR
SELECT COUNT(*) FROM note_demo WHERE grp = 3;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY(format => 'TYPICAL'));The end of the output looks like this. The exact text depends on your release.
Note
-----
- dynamic statistics used: dynamic sampling (level=2)Now gather statistics and run the same two statements again:
EXEC DBMS_STATS.GATHER_TABLE_STATS(USER, 'NOTE_DEMO');
EXPLAIN PLAN FOR
SELECT COUNT(*) FROM note_demo WHERE grp = 3;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY(format => 'TYPICAL'));The note disappears, because the optimizer no longer needs to sample. Compare the Rows estimates in the two plans. The sampled estimate is usually close but not identical, and the difference shows how much the sample affected the plan. Clean up with DROP TABLE note_demo PURGE;.
Dynamic sampling is a fallback, not a fix. A sample at level 2 reads few blocks, so skewed data can produce a poor estimate. When you see this note on a real table, check why statistics are missing. Common causes are a newly loaded table, a global temporary table, or statistics that were deleted or locked incorrectly.
Common notes and what to do
The wording changed between releases. Older releases say cardinality feedback where 12c and later say statistics feedback.
| Note text (abbreviated) | What it means | What to check |
|---|---|---|
| `dynamic statistics used: dynamic sampling (level=N)` | A table or predicate lacked usable statistics, or the optimizer chose to sample. | Gather statistics, or confirm that sampling is intended. |
| `this is an adaptive plan` | The plan can switch join method or distribution at run time. | Use the `+ADAPTIVE` modifier to see the inactive rows. |
| `statistics feedback used for this statement` | The optimizer corrected its estimates using what an earlier execution measured. | Expect different estimates on the first and later runs. |
| `SQL plan baseline "..." used for this statement` | A stored baseline constrained the plan choice. | Query `DBA_SQL_PLAN_BASELINES` to see which plan is accepted. |
| `SQL profile "..." used for this statement` | A profile adjusted the optimizer's estimates. | Query `DBA_SQL_PROFILES`. |
| `Degree of Parallelism is N because of table property` | A table or index `PARALLEL` setting caused parallel execution. | Check the table's `DEGREE` in the data dictionary. |
| `Warning: basic plan statistics not available...` | `ALLSTATS` was requested but runtime statistics were not collected. | Add the `GATHER_PLAN_STATISTICS` hint or raise `statistics_level`. |
| `'PLAN_TABLE' is old version` | Your plan table lacks columns that newer releases use. | Use the built-in global temporary plan table, or drop your private copy. |
The statistics warning in DISPLAY_CURSOR
The warning about basic plan statistics is the note you are most likely to cause yourself. It appears when you ask for ALLSTATS LAST on a statement that was not run with statistics collection:
SELECT e.last_name, d.department_name
FROM hr.employees e
JOIN hr.departments d ON d.department_id = e.department_id;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(format => 'ALLSTATS LAST'));The plan prints only E-Rows and no A-Rows, Starts or Buffers. The Note section explains why:
Note
-----
- Warning: basic plan statistics not available. These are only collected when:
* hint 'gather_plan_statistics' is used for the statement or
* parameter 'statistics_level' is set to 'ALL', at session or system levelThe fix is to run the statement again with the hint, then call DISPLAY_CURSOR again:
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;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(format => 'ALLSTATS LAST'));Adaptive plans
An adaptive plan holds more than one candidate join method. The optimizer picks one at parse time, and at run time it can switch based on the rows it actually sees. DISPLAY_CURSOR shows the final choice by default. To see the discarded branches, add +ADAPTIVE:
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(format => 'TYPICAL +ADAPTIVE'));Operations that were not used are marked with a hyphen in the first column of the plan, and the note reads this is an adaptive plan (rows marked '-' are inactive). If you do not know about this note, a plan with a hash join may appear to contradict a plan you saw earlier that used nested loops. Both can be correct for the same statement.
Using notes in a diagnosis
Treat each note as a question about the environment, not about the SQL text. The flow below shows a sensible order.
Keep these points in mind:
- A baseline or profile note means the plan you see may not be the one the optimizer would choose on its own. Changing the SQL text or gathering statistics may not change the plan.
- A feedback note means the first and second execution of one statement can have different estimates. Compare plans from the same execution stage.
EXPLAIN PLANcan print these notes, but it cannot tell you whether a baseline will actually be used at run time. Confirm withDISPLAY_CURSOR, which shows the plan that ran.
Common errors
- Reading plans with
BASICand concluding that no notes exist. Use+NOTE. - Treating dynamic sampling as proof that statistics are good. It means the opposite: statistics were absent or insufficient.
- Ignoring a baseline note and spending time rewriting SQL that a baseline will keep pinned to the same plan.
- Comparing an
E-Rowsvalue from a feedback-adjusted run with one from the first run.
Exercise
A colleague sends you a plan displayed with DBMS_XPLAN.DISPLAY(format => 'BASIC'). The plan uses a hash join, but on your system the same query uses nested loops. Which format change would you make first to look for an explanation, and what two kinds of notes might explain the difference?
Show answer
Redisplay the plan with 'BASIC +NOTE' (or TYPICAL). BASIC hides the Note section, so any hint about the environment is missing. Likely explanations are a note that a SQL plan baseline or SQL profile was used, or a note that dynamic sampling was used because statistics were missing on one system. An adaptive plan note could also explain it, since an adaptive plan can switch between nested loops and hash join at run time.
Exercise
You run DISPLAY_CURSOR with 'ALLSTATS LAST' and get only E-Rows, plus a note starting "Warning: basic plan statistics not available". Name two ways to get A-Rows and Buffers.
Show answer
Rerun the statement with the GATHERPLANSTATISTICS hint, or run ALTER SESSION SET statisticslevel = ALL before the statement. Then call DISPLAYCURSOR again. The session setting adds overhead, so use it only while diagnosing.