Sub topic 13
Looking at Past Plans with DISPLAY_AWR
Prerequisites and purpose
This chapter assumes that you can read a basic execution plan and understand sql_id, indexes, joins, and predicates. You also need access to an Oracle database with the Automatic Workload Repository (AWR) available.
DBMS_XPLAN.DISPLAY_AWR displays execution plans that Oracle saved in AWR. It helps you investigate a statement after its cursor has aged out of the shared pool. You can compare plans from different periods and check whether a plan change matches a performance change.
AWR is a repository of historical performance data. Oracle collects snapshots of information from the instance at regular intervals. The snapshots include SQL statistics and, for qualifying statements, execution plans.
Displaying a historical plan
You need the statement's sql_id. You can obtain it from monitoring records, application logs, or historical SQL views. Then pass it to DISPLAY_AWR:
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_AWR(
sql_id => '8f3k1m2n9p4q7',
format => 'TYPICAL'));The TYPICAL format shows the main plan operations, estimated rows, cost, and relevant predicate information. Oracle may return more than one plan if the statement used different plans during the period retained in AWR.
A historical plan is not an execution. The function reads a plan that Oracle previously stored. It does not prove that the plan is still present in the cursor cache, or that the optimizer would choose the same plan today.
You can request a particular plan by supplying its plan_hash_value:
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_AWR(
sql_id => '8f3k1m2n9p4q7',
plan_hash_value => 2719041836,
format => 'ALL'));A plan hash value is a number that identifies the structure of a plan. Two plans with different access paths or join methods commonly have different values. The value does not describe whether either plan is fast.
Comparing plans over time
Use historical plans alongside performance statistics. A plan change matters most when it coincides with changes in elapsed time, executions, buffer gets, or rows processed.
flowchart LR
A[Application report or log] --> B[Find sql_id]
B --> C[DISPLAY_AWR]
C --> D[Compare plan_hash_value]
D --> E[Check elapsed time and buffers]
E --> F[Investigate plan change]caption: How a historical plan supports investigation alt: A flow from finding a SQL identifier to displaying historical plans, comparing plan hashes, checking performance, and investigating a change
For deeper investigation, query historical views such as DBA_HIST_SQLSTAT and DBA_HIST_SQL_PLAN. These views require appropriate privileges. They can show when a plan appeared and how its performance changed across snapshots.
Historical plans and plan baselines
A historical plan is evidence about what Oracle used. It is not a rule that forces Oracle to use that plan.
A SQL plan baseline is a separate optimizer control. A baseline can preserve approved plans and allow Oracle to evolve plans under controlled conditions. Use DBMS_XPLAN.DISPLAY_SQL_PLAN_BASELINE to display plans stored in the SQL plan baseline repository, rather than DISPLAY_AWR.
Exercise
A statement is no longer in the shared pool, but its sql_id is known. Which function can display a previously captured plan, and what licensing requirement applies?
Show answer
Use DBMS_XPLAN.DISPLAY_AWR. It reads plans retained in AWR, which requires the Oracle Diagnostics Pack license.