You are on page 1of 35

Dec.

6th, 2007

ORACLE 11g SQL Plan Management

Inderpal S.
Johal,
President
Agenda
ƒ Execution plan and factor affecting it
ƒ How optimizer behaves without 11g Sql Plan Management
ƒ Why SQL Plan Management
ƒ 11g SQL PLAN Management Pre-Requisites
ƒ SQL Plan Management Architecture
ƒ SPM flow
ƒ Enabling SQL Plan Baselines
ƒ Loading/Dropping/Displaying SQL Plan Baseline
ƒ Space Usage/Purging for SPM
ƒ SPM View
ƒ Q&A

ORACLE 11g SMB 2


Execution Plan and factor affecting it
ƒ Execution plan define how Oracle find or writes the data to
satisfy DML statement
ƒ Execution plan define whether Oracle will use Index or
Which Index to be used and the like

Factor affecting Execution Plan and then the Business


ƒ Optimizer Version due to Database upgrade
ƒ Optimizer Statistics
ƒ Optimizer parameters
ƒ Schema definition Changes
Solution : Prior to 11g
ƒ Freeze critical plans using Stored Outlines/Profile
ƒ Freeze Statistics

ORACLE 11g SMB 3


Without SQL Plan Management ???
First Time SQL is parsed and an execution plan is generated

Optimizer Generated
Best Cost plan

GB Verified that
Plan is giving
PARSE HJ EXECUTE Acceptable
performance
HJ

ORACLE 11g SMB 4


Without SQL Plan Management ???
Database Server Environment Changed
ƒ Upgrade Database or Stats re-Gathered or Parameter Change or so
ƒ New Plan generated regardless of performance impact
ƒ Does new Plan meet required performance criteria ???

Changes Forced
Optimizer to Generate
New execution plan

GB
PARSE EXECUTE
NL Not Acceptable
PLAN
NL

ORACLE 11g SMB 5


Why SQL Plan Management
ƒ Provide Guaranteed Plan Stability
ƒ Maintain good execution plan in SQL Baseline
ƒ Only known or verified plans are used
ƒ Plans are verified
ƒ Manually
ƒ Automatically by daily maintenance Job
ƒ Only comparable or better plan are included to SQL Baseline
ƒ Main benefit is the performance stability of the system

ORACLE 11g SMB 6


11g SQL Plan Management Pre-
Requisites
ƒ Initialization Parameter Setting
1. optimizer_use_sql_plan_baselines=TRUE
[Default is TRUE]
Enable Optimizer to use SQL Plan baselines
2. optimizer_capture_sql_plan_baselines=TRUE
[ Default is FALSE]
[ Require Oracle Tuning Pack ]
Enable Oracle to Capture SQL Plan to be included in
baseline
ƒ SYSAUX Tablespace should be ONLINE

ORACLE 11g SMB 7


11g SQL Plan Management
Architecture
OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES=TRUE

GB Acceptable
PARSE
NL
EXECUTE Plan

NL

Statement log

ORACLE 11g SMB 8


SQL Plan Management Architecture
contd.
Execute the SQL query First time from SCOTT Schema
SQL> SELECT sal TESTING
FROM emp ;

Check the SQL is available in SQL PLAN Baseline


SQL> SELECT sql_text, sql_handle, optimizer_cost, enabled, fixed, accepted
FROM DBA_SQL_PLAN_BASELINES
WHERE sql_text LIKE '%TESTING%'
no rows selected

ORACLE 11g SMB 9


SQL Plan Management Architecture
contd.
OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES=TRUE

GB Acceptable
PARSE
NL
EXECUTE Plan

NL
Repeatable
SQL
statement

Statement log
Plan history

GB
Plan
NL baseline
NL

ORACLE 11g SMB 10


SQL Plan Management Architecture
contd.
Execute the SQL query SECOND time from SCOTT Schema
SQL> SELECT sal TESTING
FROM emp ;

Check the SQL is available in SQL PLAN Baseline


SQL> SELECT plan_name, sql_text, sql_handle, optimizer_cost,
enabled, fixed, accepted
FROM DBA_SQL_PLAN_BASELINES
WHERE sql_text LIKE '%TESTING%'

PLAN_NAME SQL_TEXT SQL_HANDLE OPTIMIZER_COST ENA FIX ACC


------------------------------- ------------------------------------- ----------------------------- ---------------------- ------ ----- -----
SYS_SQL_PLAN_5d47d3d select sal TESTING from emp; SYS_SQL_f9ec8ef45d 2 YES NO YES

ORACLE 11g SMB 11


SQL Plan Management Architecture
contd.
Execute the SQL query SECOND time from SCOTT Schema
SQL> SELECT * FROM TABLE ( DBMS_PLAN.DISPLAY_SQL_PLAN_BASELINE(
SQL_HANDLE=>’ SYS_SQL_f9ec8ef45d47d320',
FORMAT=> ‘BASIC'));
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL handle: SYS_SQL_f9ec8ef45d
SQL text: select sal TESTING from emp
--------------------------------------------------------------------------------
Plan name: SYS_SQL_PLAN_5d47d3d
Enabled: YES Fixed: NO Accepted: NO Origin: AUTO-CAPTURE
--------------------------------------------------------------------------------
| Id | Operation | Name |
----------------------------------
| 0 | SELECT STATEMENT | |
| 1 | SORT AGGREGATE | |
| 2 | INDEX RANGE SCAN| III |
--------------------------------------------------------------------------------

ORACLE 11g SMB 12


SQL Plan Management Architecture
contd.
Make the Change in System like Index or Collect Stats or Change
Optimizer version

GB
PARSE
HJ

HJ
Repeatable
SQL
statement

Statement log
Plan history

GB GB
Plan
HJ NL baseline
HJ NL

ORACLE 11g SMB 13


SQL Plan Management Architecture
contd.
Make the Change in System like Index or Collect Stats or Change
Optimizer version
GB
NL EXECUTE

NL
Repeatable
SQL
statement

Statement log
Plan history

GB GB
Plan
HJ NL baseline
HJ NL

ORACLE 11g SMB 14


SQL Plan Management Architecture
contd.
Execute the SQL query again from SCOTT Schema
SQL> SELECT sal TESTING
FROM emp ;

Check the SQL is available in SQL PLAN Baseline


SQL> SELECT plan_name, sql_text, sql_handle, optimizer_cost,
enabled, fixed, accepted
FROM DBA_SQL_PLAN_BASELINES
WHERE sql_text LIKE '%TESTING%'

PLAN_NAME SQL_TEXT SQL_HANDLE OPTIMIZER_COST ENA FIX ACC


------------------------------- ------------------------------------- ----------------------------- ---------------------- ------ ----- -----
SYS_SQL_PLAN_5d47d3d select sal TESTING from emp; SYS_SQL_f9ec8ef45d 2 YES NO YES
SYS_SQL_PLAN_6d1231d select sal TESTING from emp; SYS_SQL_f8de7fr6dd 1 YES NO NO

ORACLE 11g SMB 15


SQL Plan Management Architecture
contd.
SQL Repeated Several Times result High Load SQL in AWR

AWR
Repeatable
SQL GB
statement
HJ

HJ

Statement log Automatic


SQL Tuning
Plan history task

GB GB
Plan
HJ NL baseline
HJ NL

ORACLE 11g SMB 16


SQL Plan Management Architecture
contd.
SQL Repeated Several Times result High Load SQL in AWR

AWR
Repeatable
SQL GB
statement
HJ

HJ

Statement log Automatic


SQL Tuning
Plan history task

Plan
baseline GB
GB
HJ
NL
HJ
NL

ORACLE 11g SMB 17


SQL Plan Management Architecture
contd.
Execute the SQL query again from SCOTT Schema
SQL> SELECT sal TESTING
FROM emp ;

Check the SQL is available in SQL PLAN Baseline


SQL> SELECT plan_name, sql_text, sql_handle, optimizer_cost,
enabled, fixed, accepted
FROM DBA_SQL_PLAN_BASELINES
WHERE sql_text LIKE '%TESTING%'

PLAN_NAME SQL_TEXT SQL_HANDLE OPTIMIZER_COST ENA FIX ACC


------------------------------- ------------------------------------- ----------------------------- ---------------------- ------ ----- -----
SYS_SQL_PLAN_5d47d3d select sal TESTING from emp; SYS_SQL_f9ec8ef45d 2 YES NO YES
SYS_SQL_PLAN_6d1231d select sal TESTING from emp; SYS_SQL_f8de7fr6dd 1 YES NO YES

ORACLE 11g SMB 18


SQL Plan Management Architecture
contd.
Check the SQL is available in SQL PLAN Baseline
SQL> SELECT plan_name, sql_text, sql_handle, optimizer_cost,
enabled, fixed, accepted
FROM DBA_SQL_PLAN_BASELINES
WHERE sql_text LIKE '%TESTING%'
PLAN_NAME SQL_TEXT SQL_HANDLE OPTIMIZER_COST ENA FIX ACC
------------------------------- ------------------------------------- ----------------------------- ---------------------- ------ ----- -----
SYS_SQL_PLAN_5d47d3d select sal TESTING from emp; SYS_SQL_f9ec8ef45d 2 YES NO YES
SYS_SQL_PLAN_6d1231d select sal TESTING from emp; SYS_SQL_f8de7fr6dd 1 YES NO No

SQL> DECLARE
indy NATURAL;
BEGIN
indy := DBMS_SPM.ALTER_SQL_PLAN_BASELINE ('SYS_SQL_f9ec8ef45d47d320',
attribute_name => 'ACCEPTED',
attribute_value=>‘YES');
END;
/
PLAN_NAME SQL_TEXT SQL_HANDLE OPTIMIZER_COST ENA FIX ACC
------------------------------- ------------------------------------- ----------------------------- ---------------------- ------ ----- -----
SYS_SQL_PLAN_5d47d3d select sal TESTING from emp; SYS_SQL_f9ec8ef45d 2 YES NO YES
SYS_SQL_PLAN_6d1231d select sal TESTING from emp; SYS_SQL_f8de7fr6dd 1 YES NO YES

ORACLE 11g SMB 19


11g SQL Plan Selection Flow
OPTIMIZER_USE_SQL_PLAN_BASELINES=TRUE

GB

Plan part
HJ O_U_S_P_B=TRUE
Yes of history? No
HJ

Plan history
Yes
Plan
No GB baseline
GB
HJ GB
HJ
GB
HJ
HJ … HJ
HJ Plan part HJ
Yes of baseline?
HJ

No

Select baseline plan


with lowest cost

GB GB GB GB
No Yes
HJ HJ HJ HJ
>
HJ HJ HJ HJ

ORACLE 11g SMB 20


More ways to Load SQL Baseline
Already covered SQL Baseline methods
ƒ Using init.ora parameter OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES=TRUE
ƒ Using 11g Daily Automatic SQL Tuning Task
ƒ Using DBMS_SPM.ALTER_SQL_PLAN_BASELINE to move Plan from Plan History
to PLAN Baseline

More Manual method to Load SQL Baseline


ƒ Load SQL Plans from Cursor Cache
ƒ Export /Import from one Database to another

ORACLE 11g SMB 21


Enabling SQL Plan Baseline
Check the SQL is available in SQL PLAN Baseline
SQL> SELECT plan_name, sql_text, sql_handle, optimizer_cost,
enabled, fixed, accepted
FROM DBA_SQL_PLAN_BASELINES
WHERE sql_text LIKE '%TESTING%'
PLAN_NAME SQL_TEXT SQL_HANDLE OPTIMIZER_COST ENA FIX ACC
------------------------------- ------------------------------------- ----------------------------- ---------------------- ------ ----- -----
SYS_SQL_PLAN_5d47d3d select sal TESTING from emp; SYS_SQL_f9ec8ef45d 2 YES NO YES
SYS_SQL_PLAN_6d1231d select sal TESTING from emp; SYS_SQL_f8de7fr6dd 1 YES NO No

SQL>
1 SET SERVEROUTPUT ON
2 SET LONG 10000
3 DECLARE
4 report clob;
5 BEGIN
6 report := DBMS_SPM.EVOLVE_SQL_PLAN_BASELINE(
7 sql_handle => ' SYS_SQL_f8de7fr6dd ');
8 DBMS_OUTPUT.PUT_LINE(report);
9* END;
/

ORACLE 11g SMB 22


Loading SQL Plan from Cursor Cache
SQL> SELECT sql_id,sql_text FROM v$sql WHERE sql_text LIKE '%INDYTEST%‘;
SQL_ID SQL_TEXT
364s6t88vhd select sql_id,sql_text from v$sql where sql_text like '%INDYTEST%‘

Load SQL Plan from Cursor Cache


DECLARE
my_plans pls_integer;
BEGIN
my_plans := DBMS_SPM.LOAD_PLANS_FROM_CURSOR_CACHE( sql_id => 364s6t88vhd ');
END;
/

Check the SQL is available in SQL PLAN Baseline


SQL> SELECT plan_name, sql_text, sql_handle, optimizer_cost,
enabled, fixed, accepted
FROM DBA_SQL_PLAN_BASELINES
WHERE sql_text LIKE '%TESTING%'
PLAN_NAME SQL_TEXT SQL_HANDLE ORIGIN ENA ACC
------------------------------- ------------------------------------- ----------------------------- ---------------------- ------ -----
SYS_SQL_PLAN_5d47d3d select sal TESTING from emp; SYS_SQL_f9ec8ef45d MANUAL-LOAD YES YES
SYS_SQL_PLAN_6d1231d select sal TESTING from emp; SYS_SQL_f8de7fr6dd AUTO-CAPTURE YES YES

ORACLE 11g SMB 23


Drop SQL Plan from Baselines
Check the SQL is available in SQL PLAN Baseline
SQL> SELECT plan_name, sql_text, sql_handle, optimizer_cost,
enabled, fixed, accepted
FROM DBA_SQL_PLAN_BASELINES
WHERE sql_text LIKE '%TESTING%'
PLAN_NAME SQL_TEXT SQL_HANDLE ORIGIN ENA ACC
------------------------------- ------------------------------------- ----------------------------- ---------------------- ------ -----
SYS_SQL_PLAN_5d47d3d select sal TESTING from emp; SYS_SQL_f9ec8ef45d MANUAL-LOAD YES YES

Drop the Above SQL plan from the Baseline


DECLARE
drp binary_integer;
BEGIN
drp :=DBMS_SPM.DROP_SQL_PLAN_BASELINE(
SQL_HANDLE => ‘SYS_SQL_f9ec8ef45d ‘,
PLAN_NAME => ‘SYS_SQL_PLAN_5d47d3d ‘);
END;
/

ORACLE 11g SMB 24


Displaying SQL Plan Baselines
Check the SQL is available in SQL PLAN Baseline
SQL> SELECT plan_name, sql_text, sql_handle, optimizer_cost,
enabled, fixed, accepted
FROM DBA_SQL_PLAN_BASELINES
WHERE sql_text LIKE '%TESTING%'
PLAN_NAME SQL_TEXT SQL_HANDLE OPTIMIZER_COST ENA FIX ACC
------------------------------- ------------------------------------- ----------------------------- ---------------------- ------ ----- -----
SYS_SQL_d0848cb7105fffdf select sal TESTING from emp; SYS_SQL_f9ec8ef45d 2 YES NO YES
SYS_SQL_d0848cb7105fffdf select sal TESTING from emp; SYS_SQL_f8de7fr6dd 1 YES NO No

SQL> SELECT * FROM TABLE(


DBMS_XPLAN.DISPLAY_SQL_PLAN_BASELINE(
sql_handle=> ’ SYS_SQL_d0848cb7105fffdf ',format=>'basic'));

ORACLE 11g SMB 25


Disaplaying SQL Plan Baselines
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL handle: SYS_SQL_c45581c7730096e3
SQL text: select /* INDYTEST */ * from emp where job='DBA' and rownum < 50
--------------------------------------------------------------------------------
Plan name: SYS_SQL_PLAN_730096e31877483c

Enabled: YES Fixed: NO Accepted: NO Origin: AUTO-CAPTURE


--------------------------------------------------------------------------------

Plan hash value: 4187151245


-----------------------------------------------
| Id | Operation | Name |
-----------------------------------------------
| 0 | SELECT STATEMENT | |
| 1 | COUNT STOPKEY | |
| 2 | TABLE ACCESS BY INDEX ROWID| EMP |
| 3 | INDEX RANGE SCAN | EMPJOB |
-----------------------------------------------
Plan name: SYS_SQL_PLAN_730096e38694f11d

Enabled: YES Fixed: NO Accepted: YES Origin: AUTO-CAPTURE


--------------------------------------------------------------------------------

Plan hash value: 1973284518


-----------------------------------
| Id | Operation | Name |
-----------------------------------
| 0 | SELECT STATEMENT | |
| 1 | COUNT STOPKEY | |
| 2 | TABLE ACCESS FULL| EMP |
-----------------------------------
36 rows selected.

ORACLE 11g SMB 26


SQL Management Base
SYSAUX
SQL Management Base
Tablespace
Repeatable
SQL
statement Statement log
Plan history Plan history
Plan SQL
GB GB baseline GB profile
Plan GB
HJ NL baseline NL
… NL

NL Automatic
HJ NL NL SQL Tuning
task

Plan verification
before
integration to baseline

ORACLE 11g SMB 27


SQL Management Base
Three Important maintenance
ƒ Disk Space Usage
ƒ Purging Policy

SYSAUX
SQL Management Base
Tablespace

Statement log
Plan history Plan history
Plan SQL
GB GB baseline GB profile
Plan GB
HJ NL baseline NL
… NL

NL NL
HJ NL

ORACLE 11g SMB 28


Disk Space Usage
SQL Management Base Purging Policy

DISK Space Usage for Sql Management Base


ƒ By default SMB space limit is 10% of the size of SYSAUX
ƒ Allowable Limit is 1% - 50%
ƒ Warning issued in Alert log by a weekly Background process
ƒ Warning will continue every week until
¾SMB space limit is increased
DBMS_SPM.CONFIGURE( 'space_budget_percent',30);
¾Size of SYSAUX is increased
¾Space used by SMB is decreased by purging SQL baseline/Profiles
SYSAUX
SQL Management Base
Statement log
Plan history
SQL
GB GB profile
Plan
HJ NL baseline
HJ NL

ORACLE 11g SMB 29


Disk Space Usage
SQL Management Base Purging Policy

SMB Purging Policy


ƒ By default , any PLAN not used for 53 Weeks are purged
ƒ 53 Weeks ensures that Plan is available for any yearly SQL job
ƒ SMB retention range is 5 weeks – 523 weeks [ little more than 10yr]
ƒ SMB Retention period can be configured [50 weeks]
DBMS_SPM.CONFIGURE( ‘plan_retention_weeks',50);

select parameter_name, parameter_value


from dba_sql_management_config;
SYSAUX
SQL Management Base
Statement log
Plan history
SQL
GB GB profile
Plan
HJ NL baseline
HJ NL

ORACLE 11g SMB 30


Exporting/Importing SQL Baselines
TEST DATABASE PRODUCTION DATABASE

DBMS_SPM
- UNPACK_STGTAB_BASELINE
Plan history
Plan expdp impdp
baseline GB
GB
HJ
NL
HJ
GE
STABLE
NL

TA

Plan history
Plan
DBMS_SPM package baseline GB
GB
GE - PACK_STGTAB_BASELINE
STABLE HJ
TA NL
HJ
NL
DBMS_SPM package
- CREATE_STGTAB_BASELINE

ORACLE 11g SMB 31


SQL Plan Baseline Main Attributes
Plan history
ENABLED but ENABLED and
Not ACCEPTED Plan ACCEPTED
baseline
GB
GB
GB
HJ
HJ
…... NL

HJ NL
HJ

DBA_SQL_PLAN_BASELINES
Main Column Name
SQL_TEXT Æ Actual SQL Text
ORIGIN Æ MANUAL-LOAD | AUTO-CAPTURE | MANUAL-SQLTUNE |AUTO-SQLTUNE
ENABLED Æ YES | NO
ACCEPTED Æ YES | NO
FIXED Æ YES | NO
AUTOPURGE Æ YES | NO
OPTIMIZER_COST

ORACLE 11g SMB 32


DBA_SQL_PLAN_BASELINE view
Check the SQL is available in SQL PLAN Baseline
SQL> SELECT plan_name, sql_text, sql_handle, optimizer_cost,
enabled, fixed, accepted
FROM DBA_SQL_PLAN_BASELINES
WHERE sql_text LIKE '%TESTING%'
PLAN_NAME SQL_TEXT SQL_HANDLE ENABLED ACCEPTED FIXED
------------------------------- ------------------------------------- ----------------------------- -------------- -------------- ---------
SYS_SQL_d0848cb7105fffdf select sal TESTING from emp; SYS_SQL_f9ec8ef45d YES NO YES
SYS_SQL_d0848cb7105fffdf select sal TESTING from emp; SYS_SQL_f8de7fr6dd YES NO NO

DECLARE
v_chg Natural;
BEGIN
v_chg : dbms_spm.alter_sql_plan_baseline( sql_handle=>’SYS_SQL_f8de7fr6d ‘, -
plan_name=>’SYS_SQL_d0848cb7105fffdf ‘, -
attribute_name => ‘AUTOPURGE | ENABLED | ACCEPTED | FIXED’, -
attribute_value => ‘YES | NO’);
dbms_output.put_line(i);
END; Any One Attribute
Name or Value

ORACLE 11g SMB 33


QUESTIONS
ANSWERS
THANKS
indy.johal@datasoftech.com

ORACLE 11g SMB 35

You might also like