Sub topic 4
Choosing a Format: BASIC, TYPICAL, ALL and Modifiers
This chapter assumes you can already run EXPLAIN PLAN and DBMS_XPLAN.DISPLAY, and that you know a plan is a tree of operations with an Id column. You need a schema such as HR. For the V$ examples you also need privileges on the dynamic performance views. The chapter covers the format argument that all DBMS_XPLAN display functions accept, and how to pick one for a given task.
What the format argument does
DISPLAY, DISPLAY_CURSOR and DISPLAY_AWR all take a format argument, a string that controls which columns and sections appear in the output. The plan itself does not change. Only the report built from it does.
The string has two parts, separated by spaces:
- A base format, such as
BASIC,TYPICAL,ALLorADVANCED. - Zero or more modifiers. A modifier is a keyword prefixed with
+to add an item or-to remove one.
Keywords are not case sensitive. If you omit the argument, the default is TYPICAL.
EXPLAIN PLAN FOR
SELECT last_name FROM hr.employees WHERE department_id = 50;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY(format => 'BASIC +COST -BYTES'));The base formats
| Format | What it shows | Typical use |
|---|---|---|
| `BASIC` | Id, Operation and Name only | Checking the shape of the tree and the join order |
| `SERIAL` | Like `TYPICAL`, but without parallel execution details | Plans you know are serial |
| `TYPICAL` | Rows, Bytes, Cost, Time, partition and parallel columns where relevant, Predicate Information, and the Note section | Everyday plan reading |
| `ALL` | Everything in `TYPICAL`, plus Query Block Name / Object Alias and Column Projection Information | Reading plans of statements with views or subqueries, or checking which columns each step returns |
| `ADVANCED` | Everything in `ALL`, plus the outline data (the full set of hints that reproduces the plan). For cursors it also includes peeked bind values. | Reproducing a plan with hints, or checking the binds the optimizer saw |
BASIC omits the Predicate Information section. This is the most common surprise. A plan read without predicates often hides the answer to why an index was or wasn't used, so BASIC is rarely enough on its own.
What TYPICAL leaves out
TYPICAL leaves out the projection, the alias and the remote SQL sections. Those are the three items that ALL adds. When the plan involves a database link, ALL also shows the SQL sent to the remote database.
Query block names and aliases
Oracle splits a statement into query blocks, such as the main query and each subquery or inline view. Each block gets a system name like SEL$1 or SEL$2. The ALL format prints a section like this:
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$1
2 - SEL$1 / E@SEL$1You need these names when you write hints that target a specific block, for example INDEX(@SEL$1 e). The same names appear in the outline data.
Modifiers
Modifiers adjust a base format. The most useful ones are below.
| Modifier | Effect |
|---|---|
| `+PREDICATE` / `-PREDICATE` | Show or hide Predicate Information |
| `+COST` / `-COST` | Show or hide the Cost column |
| `+BYTES` / `-BYTES` | Show or hide the Bytes column |
| `+PARTITION` | Show partition pruning columns (Pstart, Pstop) |
| `+PARALLEL` | Show parallel execution columns |
| `+PROJECTION` | Show the Column Projection Information section |
| `+ALIAS` | Show the Query Block Name / Object Alias section |
| `+NOTE` / `-NOTE` | Show or hide the Note section |
| `+OUTLINE` | Show the outline data |
| `+PEEKED_BINDS` | Show the bind values used at hard parse (cursor functions) |
A few patterns follow from the table:
- Start from a small base and add what you need:
'BASIC +PREDICATE'gives a compact tree with predicates. - Start from a large base and trim:
'TYPICAL -BYTES -COST'removes columns that distract you. - Modifiers apply left to right, and the base format comes first.
EXPLAIN PLAN 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;
-- Compact tree plus the predicates
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY(format => 'BASIC +PREDICATE'));
-- Default layout without the Bytes column
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY(format => 'TYPICAL -BYTES'));
-- Which columns does each step pass up?
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY(format => 'TYPICAL +PROJECTION +ALIAS'));The last query is equivalent in content to ALL. Spelling it out with modifiers is useful when you want ALL minus one section, such as 'ALL -PROJECTION'.
Formats for runtime statistics
Runtime statistics are only available from DISPLAY_CURSOR (and DISPLAY_AWR for stored data), because EXPLAIN PLAN never runs the statement. Their formats are:
| Keyword | Adds |
|---|---|
| `IOSTATS` | Starts, A-Rows, Buffers, Reads and Writes columns |
| `MEMSTATS` | Memory use of sorts and hash operations (for example OMem, 1Mem, Used-Mem) |
| `ALLSTATS` | Shorthand for `IOSTATS MEMSTATS` |
| `LAST` | Statistics of the most recent execution only, not the cumulative total |
ALLSTATS LAST is the standard choice for tuning. Without LAST, the numbers are summed over every execution of the cursor since it was loaded. If a statement ran 50 times, the A-Rows and Buffers columns show totals over those 50 runs, and they no longer line up with the per-execution E-Rows estimate. With LAST, both columns describe one execution, so you can compare them directly.
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'));You can combine runtime keywords with other modifiers. For example, 'ALLSTATS LAST +PREDICATE +PEEKED_BINDS' gives actual rows, predicates and the bind values in one report. Add -ROWS, -BYTES or -COST if the output gets too wide for your terminal.
When the statistics columns are missing
If you ask for ALLSTATS LAST and the cursor was not run with statistics collection, the report shows only the estimated columns. Oracle usually adds a note saying that statistics were not gathered. Two causes are common:
- The statement lacked the
GATHER_PLAN_STATISTICShint, andSTATISTICS_LEVELwasTYPICAL. DISPLAY_CURSORwith nosql_idreturned a different statement than you expected. In SQL*Plus, runSET SERVEROUTPUT OFFfirst. Otherwise the last statement in your session is theDBMS_OUTPUTcall, not your query.
Choosing a format
In practice, these defaults cover most work:
- For a first look at an unexecuted statement, use
TYPICAL. - For sharing a plan in a ticket or chat, use
BASIC +PREDICATE. It is short and still shows the filters. - For anything you can actually run, use
ALLSTATS LAST. - For writing hints, use
ALLor+ALIASto get the query block names. - For reproducing a plan exactly, use
ADVANCED. Treat the outline as a diagnostic aid, not as a fix to paste into production code.
Gotchas
- Width.
ALLandALLSTATSproduce wide output. In SQL*Plus, setSET LINESIZE 200andSET PAGESIZE 1000so lines do not wrap. Wrapped plan lines are hard to read, andSET PAGESIZE 0hides the column headings. - Estimated versus actual.
formatcan add columns, but withEXPLAIN PLANyou still get estimates only. A format string cannot create runtime data that was never collected. - Cost is not time. The Cost and Time columns are estimates. Do not choose between two plans on cost alone. Run both and compare Buffers and A-Time.
- AWR formats.
DISPLAY_AWRaccepts the same format strings, but it only holds what AWR captured, and it needs the Diagnostics Pack license. Runtime statistics are generally not stored there, soALLSTATSwill not give you A-Rows from AWR.
Putting it together
The next example runs one statement and views it two ways. Compare the sections that appear in each.
SET SERVEROUTPUT OFF
SELECT /*+ GATHER_PLAN_STATISTICS */ COUNT(*)
FROM hr.employees e
WHERE e.department_id = 50;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(format => 'BASIC'));
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(format => 'ALLSTATS LAST +PREDICATE'));The first report shows only Id, Operation and Name. The second adds Starts, E-Rows, A-Rows, Buffers and A-Time, followed by the Predicate Information section. The tree is identical in both.
Exercise
You ran a query once with the GATHERPLANSTATISTICS hint. Which format string do you pass to DISPLAY_CURSOR to compare estimated and actual rows for that single execution, and why is the LAST keyword needed?
Show answer
Use 'ALLSTATS LAST'. ALLSTATS adds the runtime columns (Starts, A-Rows, Buffers and memory statistics). LAST limits them to the most recent execution. Without LAST the values are cumulative over all executions of the cursor, so they do not compare cleanly with the per-execution E-Rows estimate.
Exercise
A colleague sends you a BASIC plan and asks why an index was not used. What is missing from that report, and which format string would you ask them to run instead?
Show answer
BASIC does not print the Predicate Information section, so you cannot see the access and filter predicates. Ask for 'BASIC +PREDICATE', or for 'TYPICAL', which includes predicates along with Rows, Bytes, Cost and Time.
Exercise
You want the default TYPICAL report plus the query block names and object aliases, so you can write a hint that targets a subquery. Give a format string that does this without using ALL.
Show answer
'TYPICAL +ALIAS'. The +ALIAS modifier adds the Query Block Name / Object Alias section, which is one of the sections that ALL adds to TYPICAL.