Sub topic 13
Looking at past plans with DISPLAY_AWR
The earlier chapters in this tutorial covered EXPLAIN PLAN and DISPLAYCURSOR. Both depend on the present. EXPLAIN PLAN predicts what the optimizer would do now. DISPLAYCURSOR shows what a cursor in the shared pool did, and only while it stays there. This chapter covers the case where a statement was fast yesterday, is slow today, and its cursor has been aged out or invalidated.
It assumes you can read a plan, know what sql_id and plan_hash_value are, and have SELECT access to the DBA_HIST_* views. The AWR features here require the Oracle Diagnostics Pack license.
What AWR keeps
The Automatic Workload Repository (AWR) is a set of tables in the SYSAUX tablespace. A background process takes a snapshot of key V$ views at a regular interval and stores the differences. With default settings:
- A snapshot is taken every 60 minutes.
- Snapshots are kept for 8 days.
- Only the most resource-intensive SQL statements in each interval are stored. A cheap statement that never ranks highly may have no AWR history.
For SQL, AWR stores two things. DBA_HIST_SQLSTAT holds per-snapshot statistics such as executions, elapsed time and buffer gets. DBA_HIST_SQL_PLAN holds the plan operations, once for each distinct plan_hash_value of a sql_id.
AWR does not store row-source execution statistics. A plan from AWR therefore has no A-Rows, A-Time or per-operation Buffers. It shows the optimizer's estimates (Rows, Bytes, Cost) and the predicates, nothing more.
Finding which plans a statement used
Before calling DISPLAY_AWR, check which plans exist and when each one ran. This query joins the statistics view to the snapshot view to get readable timestamps and per-execution figures.
SELECT s.snap_id,
TO_CHAR(sn.begin_interval_time, 'YYYY-MM-DD HH24:MI') AS begin_time,
s.plan_hash_value,
s.executions_delta AS execs,
ROUND(s.elapsed_time_delta / NULLIF(s.executions_delta, 0) / 1e6, 3) AS sec_per_exec,
ROUND(s.buffer_gets_delta / NULLIF(s.executions_delta, 0)) AS gets_per_exec
FROM dba_hist_sqlstat s
JOIN dba_hist_snapshot sn
ON sn.snap_id = s.snap_id
AND sn.dbid = s.dbid
AND sn.instance_number = s.instance_number
WHERE s.sql_id = '&sql_id'
ORDER BY s.snap_id;The _DELTA columns hold the change during that snapshot interval, which is what you want. The cumulative columns (ELAPSED_TIME_TOTAL and similar) keep growing and are rarely useful here. NULLIF avoids a divide-by-zero in intervals where the statement was captured but did not complete an execution, for example a long-running statement still in progress.
A typical result shows the problem directly. If the plan_hash_value changes from one value to another at a certain snapshot and gets_per_exec jumps by a factor of 100, you have found both the time of the regression and the two plans to compare.
Displaying a historical plan
DBMS_XPLAN.DISPLAY_AWR takes the sql_id, an optional plan_hash_value, an optional db_id and a format string.
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_AWR(
sql_id => '&sql_id',
plan_hash_value => &plan_hash_value,
format => 'TYPICAL'));If you leave out plan_hash_value, Oracle prints every plan stored for that sql_id, one after another. That is convenient for a statement with two or three plans. It is noisy for one with many, so name the plan you want once you know the hashes.
The db_id parameter defaults to the current database. You only set it when the AWR data came from another database, for example after importing AWR data from production into a test system for analysis.
The format modifiers work as in the other DBMS_XPLAN functions. BASIC, TYPICAL and ALL apply, and so does +PREDICATE. Modifiers that ask for runtime columns, such as ALLSTATS LAST, have nothing to print. Do not expect A-Rows from AWR.
Comparing an old plan with a new one
The practical use of DISPLAY_AWR is comparing two plans for the same statement. Run it twice, once for each hash.
-- Plan that was running before the regression
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_AWR(
sql_id => '&sql_id',
plan_hash_value => &good_plan_hash,
format => 'TYPICAL +PREDICATE'));
-- Plan running after the regression
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_AWR(
sql_id => '&sql_id',
plan_hash_value => &bad_plan_hash,
format => 'TYPICAL +PREDICATE'));Then compare in this order:
- Access paths. Did an
INDEX RANGE SCANbecome aTABLE ACCESS FULL, or the reverse? - Join methods and join order. A
NESTED LOOPSthat became aHASH JOINchanges how the row counts scale. - Estimated rows at the leaf operations. If the estimate for one table moved from 50 rows to 5 million, look at the statistics for that table. Something changed there, such as a statistics gathering run or a data load.
- Predicate Information. Check whether a condition that was an access predicate is now a filter predicate.
Because the AWR plan has no actual rows, it cannot show that an estimate was wrong. It can only show that the estimate changed. To judge whether the new estimate was right, you need the actual counts. If the statement is still running regularly, capture the current plan with GATHER_PLAN_STATISTICS and DISPLAY_CURSOR, as in the earlier chapters.
Making sure a statement is captured
If DBA_HIST_SQLSTAT has no rows for your sql_id, the statement was probably never among the top SQL in a snapshot. For a statement you want to track, you can mark it so AWR always captures it. The call below needs privileges on the DBMS_WORKLOAD_REPOSITORY package.
EXEC DBMS_WORKLOAD_REPOSITORY.ADD_COLORED_SQL(sql_id => '&sql_id');You can also take a snapshot on demand, which is useful right after running a test, so you do not have to wait for the hourly one.
EXEC DBMS_WORKLOAD_REPOSITORY.CREATE_SNAPSHOT;To see the current interval and retention, query DBA_HIST_WR_CONTROL. Retention and interval are stored as interval values and can be changed with MODIFY_SNAPSHOT_SETTINGS, which takes minutes.
SELECT snap_interval, retention FROM dba_hist_wr_control;
EXEC DBMS_WORKLOAD_REPOSITORY.MODIFY_SNAPSHOT_SETTINGS(retention => 20160, interval => 30);That example keeps 14 days (20160 minutes) and snapshots every 30 minutes. Longer retention and shorter intervals use more SYSAUX space, so agree on changes with your DBA.
Licensing
AWR, the DBA_HIST_* views and DISPLAYAWR are part of the Oracle Diagnostics Pack, which is a separately licensed option of Enterprise Edition. The `CONTROLMANAGEMENTPACKACCESS initialization parameter controls whether the database allows use of these packs. Selecting from a DBAHIST*` view counts as use, even if you only look.
If you are not licensed, you still have these routes:
DISPLAY_CURSORfor plans that are still in the shared pool.- Statspack, the older free snapshot tool, which has its own tables for plans.
- Plan baselines, covered next.
Plans stored in SQL plan baselines
A SQL plan baseline is a set of accepted plans for a statement, kept by SQL plan management. It is a different mechanism from AWR. It records plans so the optimizer can keep using known good ones, and it is available in Enterprise Edition without the Diagnostics Pack. You display a baseline plan with DISPLAY_SQL_PLAN_BASELINE.
SELECT sql_handle, plan_name, origin, enabled, accepted, fixed
FROM dba_sql_plan_baselines
WHERE sql_text LIKE '%hr.employees%';
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_SQL_PLAN_BASELINE(
sql_handle => '&sql_handle',
plan_name => NULL,
format => 'TYPICAL'));With plan_name set to NULL, all plans in the baseline for that handle are shown. Plans that are not yet accepted are listed too, so check the ACCEPTED column to know which one the optimizer is allowed to use. If a statement changed plan despite a baseline, a new plan may be waiting as unaccepted, and the baseline view shows it.
Common problems
| Symptom | Likely cause | What to do |
|---|---|---|
| DISPLAY_AWR returns "Cannot fetch plan" or no rows | Statement not captured, or the snapshot aged out | Check `DBA_HIST_SQLSTAT`, use `ADD_COLORED_SQL` for future runs |
| ORA-00942 on `DBA_HIST_*` | Missing privilege | Ask for SELECT on the views, or the `SELECT_CATALOG_ROLE` role |
| No `A-Rows` column | AWR does not store runtime statistics | Capture a live plan with DISPLAY_CURSOR |
| Several plans printed | `plan_hash_value` omitted | Pass the hash from `DBA_HIST_SQLSTAT` |
| Plan differs from what ran | Plan was captured at a different time, or the hash covers several variants | Match by snapshot time using `DBA_HIST_SQLSTAT` |
Exercise
A report query became slow on Tuesday. DBA_HIST_SQLSTAT shows plan_hash_value 111 for Monday's snapshots and 222 from Tuesday's. You run DISPLAY_AWR for both with format 'ALLSTATS LAST' and see no A-Rows column. Why, and what would you do to compare the two plans usefully?
Show answer
AWR stores plan operations and optimizer estimates but not row-source execution statistics, so ALLSTATS LAST has nothing to print for A-Rows, A-Time or Buffers. Use TYPICAL +PREDICATE for both hashes and compare access paths, join methods, estimated rows and predicates. Use the per-execution elapsed_time_delta and buffer_gets_delta values in DBA_HIST_SQLSTAT to confirm which plan is slower. If the statement still runs, capture the current plan with GATHER_PLAN_STATISTICS and DISPLAY_CURSOR(format => 'ALLSTATS LAST') to see where actual rows diverge from estimates.
Exercise
You need to show a colleague a plan from last week, but your site does not license the Diagnostics Pack. Name two other places you could look for plan information, and one limit of each.
Show answer
One place is the shared pool with DISPLAYCURSOR, which only works while the cursor has not aged out or been invalidated. Another is a SQL plan baseline shown with DISPLAYSQLPLANBASELINE, which only exists if a baseline was captured or loaded for that statement. Statspack is also a valid answer, though it only holds plans for statements it captured at its snapshot level.