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
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
Without SQL Plan Management ???
First Time SQL is parsed and an execution plan is generated
Optimizer Generated Best Cost plan GB
PARSE
HJ HJ
EXECUTE
Verified that Plan is giving Acceptable performance
ORACLE 11g SMB
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
PARSE
GB NL NL
EXECUTE
Not Acceptable PLAN
ORACLE 11g SMB
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
11g SQL Plan Management PreRequisites 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
11g SQL Plan Management OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES=TRUE Architecture
GB
PARSE
NL NL
EXECUTE
Acceptable Plan
Statement log
ORACLE 11g SMB
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
SQL Plan Management Architecture
contd. OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES=TRUE
GB
PARSE
NL NL
EXECUTE
Acceptable Plan
Repeatable SQL statement
Statement log
Plan history
GB NL
Plan baseline
NL
ORACLE 11g SMB
10
SQL Plan Management Architecture
Execute the SQL query SECOND time from SCOTT Schema
SQL> SELECT sal TESTING FROM emp ;
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
ORACLE 11g SMB
11
SQL Plan Management Architecture
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 | --------------------------------------------------------------------------------
contd.
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 HJ HJ GB NL
Plan baseline
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 HJ HJ GB NL
Plan baseline
NL
ORACLE 11g SMB
14
SQL Plan Management Architecture
Execute the SQL query again from SCOTT Schema
SQL> SELECT sal TESTING FROM emp ;
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
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 statement GB HJ HJ
Statement log
Plan history
GB HJ HJ GB NL
Automatic SQL Tuning task
Plan baseline
NL
ORACLE 11g SMB
16
SQL Plan Management Architecture
contd. SQL Repeated Several Times result High Load SQL in AWR
AWR
Repeatable SQL statement GB HJ HJ
Statement log
Plan history
Plan baseline GB GB HJ NL HJ NL
Automatic SQL Tuning task
ORACLE 11g SMB
17
SQL Plan Management Architecture
Execute the SQL query again from SCOTT Schema
SQL> SELECT sal TESTING FROM emp ;
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
YES
NO YES
ORACLE 11g SMB
18
SQL Plan Management Architecture
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
contd.
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 ------------------------------SYS_SQL_PLAN_5d47d3d SYS_SQL_PLAN_6d1231d SQL_TEXT ------------------------------------select sal TESTING from emp; select sal TESTING from emp; SQL_HANDLE OPTIMIZER_COST ENA FIX ----------------------------- ---------------------- ------ ----SYS_SQL_f9ec8ef45d 2 YES NO SYS_SQL_f8de7fr6dd 1 YES NO ACC ----YES YES
ORACLE 11g SMB
19
11g SQL Plan Selection Flow
OPTIMIZER_USE_SQL_PLAN_BASELINES=TRUE
GB HJ HJ
O_U_S_P_B=TRUE
Yes
Plan part of history?
Yes
No
Plan history
No
GB HJ HJ GB HJ GB HJ
Plan baseline
GB HJ HJ
Yes
Plan part of baseline?
No
HJ HJ
Select baseline plan with lowest cost
GB
GB
GB
No
HJ HJ HJ HJ
Yes
GB 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 364s6t88vhd SQL_TEXT 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 -----YES ACC ----YES
SYS_SQL_PLAN_5d47d3d select sal TESTING from emp; SYS_SQL_f9ec8ef45d SYS_SQL_PLAN_6d1231d select sal TESTING from emp; SYS_SQL_f8de7fr6dd
MANUAL-LOAD
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 ----YES
SYS_SQL_PLAN_5d47d3d select sal TESTING from emp; SYS_SQL_f9ec8ef45d
MANUAL-LOAD 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 ------------------------------SYS_SQL_d0848cb7105fffdf SYS_SQL_d0848cb7105fffdf SQL_TEXT SQL_HANDLE OPTIMIZER_COST ENA FIX ACC ------------------------------------- ----------------------------- ---------------------- ------ ----- ----select sal TESTING from emp; SYS_SQL_f9ec8ef45d select sal TESTING from emp; SYS_SQL_f8de7fr6dd 2 YES 1 YES NO 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 -------------------------------------------------------------------------------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 Plan name: SYS_SQL_PLAN_730096e31877483c
Accepted: YES Origin: AUTO-CAPTURE Enabled: YES Fixed: NO -------------------------------------------------------------------------------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
Repeatable SQL statement
Tablespace
Statement log
Plan history
GB HJ HJ GB NL
Plan history
Plan baseline GB GB NL NL NL NL SQL profile
Plan baseline
NL
Automatic 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 Statement log
Plan history
GB HJ HJ GB NL
Tablespace
Plan history
Plan baseline GB GB NL NL NL NL SQL profile
Plan baseline
NL
ORACLE 11g SMB
28
SQL Management Base
Disk Space Usage 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
SQL Management Base Statement log Plan history
GB HJ HJ GB NL
SYSAUX
Plan baseline
SQL profile
NL
ORACLE 11g SMB
29
SQL Management Base
Disk Space Usage 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;
SQL Management Base Statement log Plan history
GB HJ HJ GB NL
SYSAUX
Plan baseline
SQL profile
NL
ORACLE 11g SMB
30
Exporting/Importing SQL Baselines
TEST DATABASE PRODUCTION DATABASE DBMS_SPM - UNPACK_STGTAB_BASELINE Plan history Plan baseline
expdp
GB HJ
impdp
GB NL
NL
HJ
GE STABLE TA
Plan history DBMS_SPM package - PACK_STGTAB_BASELINE Plan baseline
GB HJ NL NL HJ
GE STABLE TA
GB
DBMS_SPM package - CREATE_STGTAB_BASELINE
ORACLE 11g SMB
31
SQL Plan Baseline Main Attributes
Plan history
ENABLED but Not ACCEPTED
GB HJ HJ HJ
Plan baseline
GB GB
ENABLED and ACCEPTED
...
HJ
NL NL
DBA_SQL_PLAN_BASELINES
Main Column Name SQL_TEXT ORIGIN ENABLED ACCEPTED FIXED AUTOPURGE OPTIMIZER_COST Actual SQL Text MANUAL-LOAD | AUTO-CAPTURE | MANUAL-SQLTUNE |AUTO-SQLTUNE YES | NO YES | NO YES | NO YES | NO
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 ------------------------------SYS_SQL_d0848cb7105fffdf SYS_SQL_d0848cb7105fffdf SQL_TEXT SQL_HANDLE ENABLED ACCEPTED FIXED ------------------------------------- ------------------------------------------ -------------- --------select sal TESTING from emp; SYS_SQL_f9ec8ef45d select sal TESTING from emp; SYS_SQL_f8de7fr6dd YES YES NO NO YES 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); Any One Attribute END;
Name or Value
ORACLE 11g SMB
33
QUESTIONS ANSWERS
THANKS
[email protected]
ORACLE 11g SMB
35