Bajwa Consulting Need senior Oracle DBA help? Performance tuning, Cloud migrations, GoldenGate, backup and DR. Plus 70+ free DBA scripts.
Visit bajwaconsulting.com →

Friday, April 25, 2014

SQL plan management (cleanup of Sysaux)

Originally posted in 2014. Updated November 2026 for Oracle Database 19c and 21c.

SQL Plan Management (SPM) lets the optimizer use only execution plans you have accepted, so a statistics refresh or an upgrade cannot quietly swap a good plan for a bad one. New plans are recorded, tested and accepted only if they perform better. The plans live in the SQL Management Base (SMB) in the SYSAUX tablespace, and if nobody manages them, SYSAUX keeps growing. This post covers what to check and what to run regularly on 19c and 21c.

How SPM works

  1. Capture: plans are stored as SQL plan baselines. The first plan for a statement is accepted, and later plans are added to the plan history as unaccepted.
  2. Selection: for a statement that has a baseline, the optimizer uses only accepted plans. Any new plan it finds is recorded but not used.
  3. Evolution: unaccepted plans are test-run and accepted only if they perform better. Since 12c this happens automatically every night through the SYS_AUTO_SPM_EVOLVE_TASK in the maintenance window.

1. Capture only what you need

Setting OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES=TRUE for the whole database captures a baseline for every repeatable statement, and that is what fills SYSAUX. On 19c and 21c, either load plans for the statements you care about, or keep automatic capture but limit it with filters.

-- Load the current plan of one known-good statement from the cursor cache
DECLARE
  n PLS_INTEGER;
BEGIN
  n := DBMS_SPM.LOAD_PLANS_FROM_CURSOR_CACHE(sql_id => '&sql_id');
  DBMS_OUTPUT.PUT_LINE(n || ' plan(s) loaded');
END;
/

-- Or capture automatically, but only for one application schema
EXEC DBMS_SPM.CONFIGURE('AUTO_CAPTURE_PARSING_SCHEMA_NAME', 'APP_OWNER', TRUE);
ALTER SYSTEM SET optimizer_capture_sql_plan_baselines = TRUE;

2. Check how much space SPM uses

SELECT occupant_name, ROUND(space_usage_kbytes/1024) AS mb
FROM   v$sysaux_occupants
WHERE  occupant_name = 'SQL_MANAGEMENT_BASE';

SELECT parameter_name, parameter_value, last_modified
FROM   dba_sql_management_config
WHERE  parameter_name IN ('SPACE_BUDGET_PERCENT','PLAN_RETENTION_WEEKS');

SELECT accepted, enabled, COUNT(*) AS plans
FROM   dba_sql_plan_baselines
GROUP  BY accepted, enabled;

The defaults are still SPACE_BUDGET_PERCENT = 10 (allowed range 1 to 50) and PLAN_RETENTION_WEEKS = 53 (allowed range 5 to 523). When the SMB goes over its space budget, Oracle writes a warning to the alert log every week. It does not stop capturing plans.

3. Shorten retention and set the space budget

The automatic purge removes plans that have not been used for PLAN_RETENTION_WEEKS, whether they are accepted or not. It runs as part of the nightly maintenance tasks. For most systems, half a year is enough:

EXEC DBMS_SPM.CONFIGURE('plan_retention_weeks', 26);
EXEC DBMS_SPM.CONFIGURE('space_budget_percent', 20);

4. Drop unaccepted plans safely

Correction to my 2014 version: calling DBMS_SPM.DROP_SQL_PLAN_BASELINE with only sql_handle drops every plan for that statement, accepted plans included. Always pass plan_name as well:

SET SERVEROUTPUT ON
DECLARE
  n       PLS_INTEGER;
  total   PLS_INTEGER := 0;
BEGIN
  FOR r IN (SELECT sql_handle, plan_name
            FROM   dba_sql_plan_baselines
            WHERE  accepted = 'NO'
            AND    fixed    = 'NO'
            AND    last_verified IS NOT NULL      -- already evaluated and rejected
           ) LOOP
    n := DBMS_SPM.DROP_SQL_PLAN_BASELINE(sql_handle => r.sql_handle,
                                         plan_name  => r.plan_name);
    total := total + n;
  END LOOP;
  DBMS_OUTPUT.PUT_LINE(total || ' unaccepted plan(s) dropped');
END;
/

Leave out the last_verified line if you also want to drop plans the evolve task has not tested yet.

5. Review the automatic evolve task

SELECT parameter_name, parameter_value
FROM   dba_advisor_parameters
WHERE  task_name = 'SYS_AUTO_SPM_EVOLVE_TASK'
AND    parameter_value <> 'UNUSED'
ORDER  BY parameter_name;

-- Report of the last run
SELECT DBMS_SPM.REPORT_AUTO_EVOLVE_TASK FROM dual;

If you would rather approve plans yourself, set ACCEPT_PLANS to FALSE:

EXEC DBMS_SPM.SET_EVOLVE_TASK_PARAMETER('SYS_AUTO_SPM_EVOLVE_TASK', 'ACCEPT_PLANS', 'FALSE');

Multitenant note

In a 19c or 21c container database, each PDB has its own SQL Management Base and its own settings. Run the checks and the DBMS_SPM.CONFIGURE calls while connected to each PDB, not only in CDB$ROOT. The user needs the ADMINISTER SQL MANAGEMENT OBJECT privilege.

Routine checklist

  • Monthly: check SMB size in V$SYSAUX_OCCUPANTS and look for SPM space warnings in the alert log.
  • Monthly: read the auto evolve report and drop rejected plans.
  • After upgrades or big releases: load known-good plans for critical SQL from the cursor cache or a SQL tuning set.

More current DBA scripts are in my GitHub repository: github.com/sbajwa55/dba-scripts. For help with Oracle performance, migrations or AWS, visit Bajwa Consulting Services.

No comments:

Post a Comment