List Oracle SQL Plan Baselines with DBA_SQL_PLAN_BASELINES

List Oracle SQL Plan Baselines with DBA_SQL_PLAN_BASELINES

Purpose

DBA_SQL_PLAN_BASELINES shows every plan that SQL plan management holds for the database: the statement it belongs to, how it got there, and whether the optimizer is allowed to use it. A SQL plan baseline is a set of accepted plans that the optimizer may use for one SQL statement. When a baseline exists, the optimizer uses an accepted plan from the baseline, and it stores any new plan as unaccepted until that plan is verified.

So a statement that keeps its old plan after you add an index or gather new statistics can have a baseline behind it, and this view is where you check. The queries below list the baselines, show the plan stored for one statement, load a plan from the cursor cache, and drop a baseline that is no longer wanted.

Code

 1-- 1. List baselines and their status for one statement
 2SELECT SQL_HANDLE, SQL_TEXT, PLAN_NAME,
 3       ORIGIN, ENABLED, ACCEPTED, FIXED
 4FROM   DBA_SQL_PLAN_BASELINES
 5WHERE  SQL_TEXT LIKE '%q1_group%';
 6
 7-- 2. Show the baseline plan a cached statement is using
 8SELECT PLAN_TABLE_OUTPUT
 9FROM   V$SQL s, DBA_SQL_PLAN_BASELINES b,
10       TABLE(
11       DBMS_XPLAN.DISPLAY_SQL_PLAN_BASELINE(b.sql_handle,b.plan_name,'basic')
12       ) t
13WHERE  s.EXACT_MATCHING_SIGNATURE=b.SIGNATURE
14AND    b.PLAN_NAME=s.SQL_PLAN_BASELINE
15AND    s.SQL_ID='31d96zzzpcys9';
16
17-- 3. Load the current plan of a cached statement as a baseline
18SELECT SQL_ID, CHILD_NUMBER AS "Child Num",
19       PLAN_HASH_VALUE AS "Plan Hash",
20       OPTIMIZER_ENV_HASH_VALUE AS "Opt Env Hash"
21FROM   V$SQL
22WHERE  SQL_TEXT LIKE 'SELECT /*LOAD_CC*/%';
23
24VARIABLE v_plan_cnt NUMBER
25BEGIN
26  :v_plan_cnt:=DBMS_SPM.LOAD_PLANS_FROM_CURSOR_CACHE(
27    sql_id => '27m0sdw9snw59');
28END;
29/
30
31-- 4. Drop every plan for one SQL handle
32DECLARE
33  v_dropped_plans number;
34BEGIN
35  v_dropped_plans := DBMS_SPM.DROP_SQL_PLAN_BASELINE (
36     sql_handle => 'SQL_b6b0d1c71cd1807b'
37);
38  DBMS_OUTPUT.PUT_LINE('dropped ' || v_dropped_plans || ' plans');
39END;
40/

Code Breakdown

Query 1: the baseline inventory

DBA_SQL_PLAN_BASELINES has one row for each plan. The columns in this query are the ones a DBA reads first:

  • SQL_HANDLE identifies the statement as a string. Every plan for the same statement shares it, and the DBMS_SPM functions take it as their main input.
  • SQL_TEXT is the statement text as it was parsed, not normalized. It is a CLOB. The LIKE filter on a comment string such as q1_group is how the query finds one statement; replace it with a word from your own statement.
  • PLAN_NAME identifies one plan inside the baseline.
  • ORIGIN says how the plan got there. AUTO-CAPTURE means the optimizer captured it; MANUAL-LOAD-FROM-CURSOR-CACHE, MANUAL-LOAD-FROM-AWR and MANUAL-LOAD-FROM-STS mean someone loaded it from the shared SQL area, from AWR or from a SQL tuning set. In 19c the column can hold 14 values in all, among them the EVOLVE- origins.
  • ENABLED, ACCEPTED and FIXED are YES or NO; Key Points covers how they combine.

To see every baseline, remove the WHERE clause. To see the baselines of one schema, filter on PARSING_SCHEMA_NAME.

Query 2: the plan stored in the baseline

DBMS_XPLAN.DISPLAY_SQL_PLAN_BASELINE prints a stored plan from its SQL_HANDLE and PLAN_NAME. The join finds those two values for a statement that is in the cursor cache:

  • V$SQL.EXACT_MATCHING_SIGNATURE matches DBA_SQL_PLAN_BASELINES.SIGNATURE, the identifier built from the normalized SQL text.
  • V$SQL.SQL_PLAN_BASELINE holds the name of the baseline plan the cursor used, so it matches PLAN_NAME.
  • 'basic' is the format: the minimum detail, meaning the operation ID, the operation name and its option.

The output starts with a header that repeats the SQL handle, the plan name, and the Enabled, Fixed, Accepted and Origin flags, then prints the plan. Replace 31d96zzzpcys9 with your own SQL ID. If the query returns no rows, the cursor did not use a baseline, or the statement is no longer in the cursor cache.

Query 3: load a plan from the cursor cache

The first SELECT finds the SQL ID of a statement in the shared SQL area. The /*LOAD_CC*/ comment in the example is a marker placed in the statement so it is easy to find. The PL/SQL block then passes that SQL ID to DBMS_SPM.LOAD_PLANS_FROM_CURSOR_CACHE, which loads the statement's plans into its baseline and returns the number of plans loaded into the bind variable v_plan_cnt.

A plan loaded by hand is marked accepted at once. The database assumes that a plan a DBA chose to load performs well enough. Its ORIGIN begins with MANUAL-LOAD. The function also takes a fixed parameter: the default NO loads the plan as a nonfixed plan.

Query 4: drop a baseline

DBMS_SPM.DROP_SQL_PLAN_BASELINE takes a sql_handle, a plan_name, or both. With only the handle, as here, plan_name stays NULL and the function drops every plan for that statement, which removes the baseline. It returns the number of plans dropped, and DBMS_OUTPUT.PUT_LINE prints that number. Run query 1 again afterwards; it should return no rows for the statement.

Key Points

  • Accepted, enabled and fixed are separate flags. An accepted plan is in the baseline and available to the optimizer. A disabled plan cannot be used even when it is accepted. A fixed plan is an accepted plan marked as preferred: when a baseline holds fixed plans, the optimizer uses the fixed plan with the lowest cost. If the baseline contains at least one enabled fixed plan, the optimizer cannot use plans added later until you mark them fixed as well.
  • Baselines are on by default, automatic capture is off. OPTIMIZER_USE_SQL_PLAN_BASELINES defaults to true. OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES defaults to false, so the optimizer does not create baselines for repeated statements unless someone turns capture on.
  • LAST_EXECUTED lags. The column is not updated after every execution, so a baseline may have run more recently than it shows.
  • Standard Edition 2 limits SQL plan management. On SE2 each SQL statement keeps only one SQL plan baseline, alternative plans are not added to the plan history, unused baselines are not auto-purged, and plan evolution is disabled. DBMS_SPM.CONFIGURE, LOAD_PLANS_FROM_AWR, LOAD_PLANS_FROM_SQLSET and every evolution subprogram are not allowed. Listing baselines, loading from the cursor cache, and ALTER_SQL_PLAN_BASELINE and DROP_SQL_PLAN_BASELINE all work.
  • The baselines live in SYSAUX. They sit in the SQL management base, part of the data dictionary in the SYSAUX tablespace. If SYSAUX is unavailable, the database does not use SQL plan management.
  • DBMS_SPM needs a system privilege. A user with ADMINISTER SQL MANAGEMENT OBJECT can run the package.

Insights and Best Practices

Read ORIGIN before you change anything

A baseline from AUTO-CAPTURE came from the optimizer itself. One from a MANUAL-LOAD origin was put there on purpose by a person or a script. Before you drop or disable a manual baseline, find out who loaded it and why: the CREATOR, CREATED and DESCRIPTION columns help. Dropping it can bring back the slow plan it was loaded to prevent.

Watch for unaccepted plans

Rows with ACCEPTED = 'NO' are plans the optimizer found but has not been allowed to use. On SE2 there are none, because alternative plans are not added to the plan history. Outside SE2, the automatic task SYS_AUTO_SPM_EVOLVE_TASK runs daily in the maintenance window: it selects and ranks unaccepted plans for verification, and accepts each plan that meets the performance threshold. When a statement collects unaccepted plans that never get accepted, compare them with the accepted plan using query 2's DISPLAY_SQL_PLAN_BASELINE call, passing each PLAN_NAME.

Disable before you drop

DBMS_SPM.ALTER_SQL_PLAN_BASELINE changes one attribute of a plan, or of all plans for a SQL handle. Setting enabled to NO stops the optimizer using a plan while keeping it, so you can switch it back with one more call. The same function sets fixed, autopurge and description, and returns the number of plans it changed.

Plan the space and the purge

On SE2, unused baselines are not auto-purged and DBMS_SPM.CONFIGURE is not allowed, so the settings below do not apply there. The SQL management base has a space budget, SPACE_BUDGET_PERCENT, which defaults to 10% of SYSAUX. When it is exceeded, the database writes a warning to the alert log, and repeats it weekly until space is freed or the limit is raised. Plans that have not run for longer than PLAN_RETENTION_WEEKS are purged; the default is 53 weeks, and the range is 5 to 523. A plan with AUTOPURGE = 'NO' is never purged. Both settings are in DBA_SQL_MANAGEMENT_CONFIG and are changed with DBMS_SPM.CONFIGURE.

Do not confuse SQL plan baselines with AWR baselines

An AWR baseline is a saved range of AWR snapshots for comparing workloads; see listing AWR baselines with DBA_HIST_BASELINE. A SQL plan baseline is a set of accepted plans for one statement. To see the plan a statement actually ran, rather than the one stored in its baseline, see getting the actual execution plan with DBMS_XPLAN.DISPLAY_CURSOR.

When to Run This

  • A query keeps the same plan after you add an index or gather new statistics.
  • After an upgrade, to see which statements are pinned to plans captured on the old release.
  • Before you move baselines between databases, to list what exists on the source.
  • When the alert log warns that the SQL management base has passed its space budget.
  • After you load a plan by hand, to confirm it shows ACCEPTED = 'YES' with the origin you expect.

Troubleshooting Common Issues

Query 2 returns no rows. Check V$SQL.SQL_PLAN_BASELINE for the SQL ID. If it is NULL, the cursor did not use a baseline: the statement may have no baseline, its plans may all be disabled, or OPTIMIZER_USE_SQL_PLAN_BASELINES may be false for the session. Also check that the statement is still in the cursor cache.

A loaded plan is not being used. Run query 1 and read ENABLED and ACCEPTED. If another plan for the same handle has FIXED = 'YES' (see Key Points), either fix the new plan or unfix the old one with ALTER_SQL_PLAN_BASELINE. Also check REPRODUCED: NO means the optimizer was not able to reproduce that plan.

DBMS_SPM calls fail with a privilege error. The account needs ADMINISTER SQL MANAGEMENT OBJECT.

The plan display is slow or shows a different plan for an old baseline. From Oracle Database 12c the SQL management base stores the plan itself. For a plan created before 12c, DISPLAY_SQL_PLAN_BASELINE must compile the statement and generate the plan, because the SQL management base does not store it.

References

Posts in this series