2017-620 HowTo SQL Plan Management in Oracle 1ig(OBMS_SPM) - Managed Hosting Internal - Confluence Production
HowTo - SQL Plan Management in Oracle 11g
(BBMS= SPM) mites by Sreedhara Chidambaram on Jul 01, 2013
Introduction
When a SQL statement is hard parsed, the cost based optimizer produces several execution plans and selects the one
with the lowest cost, If a SQL plan baseline is present, the optimizer compares the plan it just produced with the plans in
the SQL plan baseline. If a matching plan is found that is flagged as accepted the plan is used. If the SQL plan baseline
doesn't contain an accepted plan matching the one it just created, the optimizer evaluates the accepted plans in the SQL
plan baseline and uses the one with the lowest cost. If the execution plan originally produced by the optimizer has a lower
cost than those present in the SQL plan baseline, it is added to the baseline as a not-accepted plan, so it is not used until it
is verified not to cause a reduction in performance. If a system change affects all ex'sting accepted plans, so they are
considered non-reproducible, the optimizer will use the plan with the lowest cost.
‘The use of SQL plan baselines is controlled by the OPTIMIZER_USE_SQL_PLAN_BASELINES parameter, which is set to
TRUE by default.
Automatic Plan Capture
‘The value of the OPTIMIZER_CAPTURE_SQL,PLAN_BASELINES parameter, whose defauit value is FALSE, determines
if the system should automatically capture SQL plan baselines. When set to TRUE, the system records a plan history for
SQL statements. The first plan for a specific statement is automatically flagged as accepted, Alternative plans generated
after this point are not used until tis verified they do not cause performance degradations, Plans with acceptable
performance are added to the SQL plan baseline during the evolution phase.
SQL> SHOW PARAMETER OPTINIZER_CAPTURE_SQL_PLAN BASELINES
NAME TYPE VALUE
optimizer_capture_sql_plan_baselines boolean FALSE
SQL> ALTER SYSTEM SET OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES=TRUE;
system altered.
SQL>
I would advise doing considerable testing before using automatic plan capture in a production environment.
Manual Plan Loading
‘Another way to create SQL plan baselines is by manually loading existing plans for a set of SQL statements as SQL plan
baselines. The manually loaded plans are not verified for performance, but are added as accepted plans to existing or new
SQL plan baselines. You can use manual plan loading in conjunction with or as an altemative to automatic plan capture,
Manual plan loading can be performed by:
Loading Plans from SQL Tuning Sets and AWR Snapshots
Loading Plans from the Cursor Cache
Loading Plans from SQL Tuning Sets and AWR Snapshots
To load plans from a SQL Tuning Set, use the LOAD_PLANS_FROM_SQLSET function of the DBMS_SPM package:
DECLARE
ny_plans pls_integer;
BEGIN
hip ieouence hops 0 8443fpaqesviewpage.actionpageld=21258815 16worse HowTo SOL Pan Managemertin rset (OBMS_SPM)- Manage Hosta ral = Conuene Praicion
ny_plans = DBMS_S°M, LOAD_PLANS_FROM_SQLSET(
Sqlset_nane => *tset1")}
ENDS
J
‘To load plans from Automatic Workload Repository (AWR), load the plans stored in AWR snapshots into a SQL Tuning Set
before using the LOAD_PLANS_FROM_SQLSET function as described in this section,
Loading Plans from the Cursor Cache
To load plans from the cursor cache, use the LOAD_PLANS_FROM_CURSOR_CACHE function of the OBMS_SPM
package:
SET SERVEROUTPUT ON
DECLARE
Lplans_loaded PLS_INTEGER;
BEGIN
1_plans_loaded := DBMS_SPM.load_plans_from_cursor_cache(
sql_id => '€&1');
DBMS_OUTPUT.put_line("Plans Loaded: ' || 1_plans_loaded);
END;
/
To check the baselines created:
set linesize 200;
SELECT sql_handle, plan_name, enabled, accepted, fixed, created, last_executed FROM dba_sql_plan_ba
Evolving SQL Plan Baselines
Evolving a SQL plan baseline is the process by wi
should be accepted.
‘As mentioned previously, manually loaded plans are automatically marked as accepled, so manual loading forces the
evolving process.
When plans are loaded automatically, the baselines are evolved using the EVOLVE_SQL_PLAN_BASELINE function,
which returns a CLOB reporting its results.
the optimizer determines if non-accepted plans in the baseline
SET LONG 10000
SELECT DBMS_SPM.evolve_sql_plan_baseline(sql_handle
"SYS_SQL_7676323ad90440b9') FROM dual;
Altering Plan Baselines
‘The ALTER_SQL_PLAN_BASELINE function allows the following attributes of a specific plan, or all plans within a baseline
to be altered:
‘enabled (YES/NO) : If YES, the plan is available for the optimizer if it is also marked as accepted.
fixed (YES/NO) : IT YES, the SQL plan baseline will not evolve over time. Fixed plans are used in preference to non-fixed
plans.
autopurge (YES/NO) : If YES, the SQL plan baseline is purged automatically if itis not used for a period of time.
plan_name : Used to amend the SQL plan name, up to a maximum of 30 character.
description : Used to amend the SQL plan description, up to a maximum of 30 character.
To fix a specific plan for a sql_id:
hip ieouence hippo 8443pagesviewpage.acion?pageld=21258815 262017-5620 HowTo SQL Plan Management in Oracle 1ig(DBMS_SPM) - Managed Hosting Internal - Confluence Production
SET SERVEROUTPUT ON
DECLARE
1 _plans_altered PLS_INTEGER;
BEGIN
1 _plans_altered := DBMS_SPM,alter_sql_plan_baseline(
sql_handle > BRL",
plan_name *ag2",
attribute_name => ‘fixed’,
attribute_value => 'YES');
DBMS_OUTPUT.put_line(‘Plans Altered: ' || 1_plans_altered);
END;
/
‘Sql_handle and plan_nane can be obtained from the below query
set linesize 200;
SELECT sql_handle, plan_name, enabled, accepted, fixed, created, last_executed FROM dba_sql_plan_ba
Displaying SQL Plan Baselines
In addition to querying the DBA_SQL_PLAN_BASELINES view, information about SQL plan baselines is available via the
DBMS_XPLAN package.
‘The DISPLAY_SQL_PLAN_BASELINE table function displays formatted information about a specific plan, or all plans in
the SQL plan Baseline in one of three formats (BASIC, TYPICAL or ALL).
SET SERVEROUTPUT ON
select * from table(doms_xplan.display_sql_plan_baseline(sql_handle=>'&&1' ,format=>'basic'));
SQL Management Base
‘The SQL management base resides in the SYSAUX tablespace and stores SQL plan baselines, as well as statement logs,
plan histories and SQL profiles. Space usage is controlled by altering two name-value attributes using the CONFIGURE
procedure of the DBMS_SPM package.
space_budget_porcent (default 10) : Maximum size as a percentage of SYSAUX space, Allowable values 1-50,
plan_retention_weeks (default 53) : Number of weeks unused plans are retained before being purged. Allowable values 5-
523 weeks.
‘The current settings are visible using the DBA_SQL_MANAGEMENT_CONFIG view.
‘SQL> SELECT parameter_name, parameter_value FROM cba_sql_managenent_config;
PARAMETER_NAME PARAMETER_VALUE
SPACE_BUDGET_PERCENT 10
PLAN_RETENTION_WEEKS 53
2 rows selected.
‘To modify the above values:
‘The following exanple shows both values being reset.
BEGIN
DBMS_SPM. configure("space_budget_percent’, 11);
hip ieouence hops 0 8443fpaqesviewpage.actionpageld=21258815 362017-5620 HowTo SQL Plan Management in Oracle 1ig(OBMS_SPM) - Managed Hosting Internal - Confluence Production
DBMS_SPM. configure(‘plan_retention_weeks’, 54);
END;
/
SQL> SELECT parameter_name, parameter_value FROM dba_sql_management_config;
PARAMETER_NAME PARAMETER_VALUE
‘SPACE_BUDGET_PERCENT
a
PLAN_RETENTION_WEEKS
54
2 rows selected.
Transferring SQL Plan Baselines
The DBMS_SPM package provides functionality for transferring SQL. plan baselines between databases. First, a staging
table must be created in the source database using the CREATE_STGTAB_BASELINE procedure
BEGIN
DBMS_SPM.CREATE_STGTAB_BASELINE(
‘table_nane “Spm_stageing tab",
table_owner ‘Test’,
tablespace_name => ‘USERS');
END;
/
SQL>
‘The PACK_STGTAB_BASELINE function exports the SQL plan baselines to the staging table, There are several
parameters allowing you to limit amount and type of data you export. The following example exports all SQL plan baselines.
SET SERVEROUTPUT ON
DECLARE
L_plans_packed PLS_INTEGER;
BEGIN
L_plans_packed :
‘table_nane
‘table_owner
DBMS_SPM. pack_stgtab_baseline(
"spm_stageing tab",
“TEST');
DBMS_OUTPUT.put_line( ‘Plans Packed:
END;
/
Plans Packed: 11
I] 1_plans_packed);
PL/SQL procedure successfully completed.
SQL>
The staging table is then transferred to the destination database using data pump or the original export/import utilities. Once
in the destination database, the SQL plan baselines are imported into the dictionary using the
UNPACK_STGTAB_BASELINE function, Once again, there are several parameters allowing you to limit amount and type
of data you import. The following example imports all SQL plan baselines owned by the user "TEST"
SET SERVEROUTPUT ON
DECLARE
1_plans_unpacked PLS_INTEGER;
BEGIN
1_plans_unpacked
DBMS_SPM.unpack_stgtab_baseline(
hip ieouence hops 0 8443fpaqesviewpage.actionpageld=21258815 462017-590 HowTo- SOL Plan Managemertin Oracle 11g (DBMS_SPM) - Managed Hosting Ileral - Confluence Production
‘table_nane => ‘spm_stageing_ tab’,
‘table_owner ‘TEST’,
creator ‘TEST')5
DBMS_OUTPUT.put_line( ‘Plans Unpacke;
END;
/
Plans Unpacked: 11
PL/SQL procedure successfully completed.
SQL>
Dropping Plans and Baselines
‘The DROP_SQL_PLAN_BASELINE func!
specified.
SET SERVEROUTPUT ON
DECLARE
Lplans_dropped PLS_INTEGER;
BEGIN
1 plans_dropped := DBMS_SPM.drop_sql_plan_baseline (
sql_handle => ‘88a",
plan_name => NULL);
DBMS_OUTPUT. put.
END;
/
ine(1_plans_dropped) ;
NB:
have attached the ppt slides and sample scripts used for explaining the DBMS_SPM during our presentation, Also, the
recordings of the presentation can be found here
https: //sas-elluminate. com/p. jnlp?psid=2013-03-13.0757.M.CFBS@B248DEC482C4B85D8D397418A..vcr&sii
‘You can use below SQL script to lock a good plan for a SQL:
lock_good_plan_using_spm.sql
+ Introduction
+ Automatic Plan Capture
+ Manual Plan Loading
+ Loading Plans ‘rom SQL Tuning Sets and AWR Snapshots
+ Loading Plans ‘rom the Cursor Cache
Evolving SQL Plan Baselines
Altering Plan Baselines
Displaying SQL Plan Baselines
SQL Management Base
Transferring SQL Plan Baselines
Dropping Plans and Baselines
1 Comment
hips ileonuence hps.o 8444paqes\vlewpage.action?pageld=213258816
"IL ALptans_unpacked) ;
ccan drop a specific plan from a baseline, or all plans if the plan name is not
No labels
562017-620 HowTo- SQL Plan Management in Oracle 1ig(DBMS_SPM) - Managed Hosting Internal - Confluence Production
Senthil Jayaraman
For lock or to Fix the Plan we can consider the OPTIMIZER_COST to choose the PLAN_NAME. so that we can
avoid the confusion. | have used this method in Indiana Wesleyan University
SQL> SELECT sql_handle,plan_name, enabled, accepted, fixed, EXECUTIONS, OPTIMIZER_COST, ROWS
SQL_HANDLE PLAN_NAME ENA ACC FIX EXECUTIONS OPT
SQL_90dd42710763475¢ SQL_PLAN_91raaf7mq6juz5e527ed9 YES YES NO 2
SQL_90dd42710763475¢ SQL_PLAN_91raaf7mq6juz971#27a9 YES YES YES 167931
SQL>
hip ieouence hippo 8443pagesviewpage.acion?pageld=21258815
66