1.1 相关名词说明SQL Plan Management(SPM):oracle11g 中提供的新特性,用来更好地控制执行计划。
Plan History:优化器生成的所有执行计划的总称。
SQL Plan Baseline: Plan History里那些被标记为“ACCEPTED”的执行计划的总称。
Plan Evolution:把一条执行计划从Plan History里标记为“ACCEPTED”的过程。
SQL Management Base(SMB): 字典表里保存的执行计划的总称,包括Plan History,SQL Plan Baseline和SQL profile。
语法:
DBMS_SPM.ALTER_SQL_PLAN_BASELINE (
sql_handle IN VARCHAR2 := NULL,
plan_name IN VARCHAR2 := NULL,
attribute_name IN VARCHAR2,
attribute_value IN VARCHAR2 )
RETURN PLS_INTEGER;
如把某个baseline 标记为FIXED:
SET SERVEROUT ON;
DECLARE
x NUMBER;
BEGIN
x := DBMS_SPM.ALTER_SQL_PLAN_BASELINE (
sql_handle => '&&sql_handle',
plan_name => '&&plan_name',
attribute_name => 'FIXED',
attribute_value => 'YES' );
END;
/
1.8 相关MOS 文档Whitepaper: SQL Plan Management in Oracle Database 11g
Loading Hinted Execution Plans into SQLPlan Baseline. [ID 787692.1]
How to Use SQL Plan Management (SPM) -Example Usage (Doc ID 456518.1)
Plan Stability Features (Including SPM) Start Point (Doc ID 1359841.1)
HOW TO LOAD SQL PLANS INTO SPM FROM AWR (Doc ID 789888.1)
Sql Plan Baseline Not always created (Doc ID 788853.1)
Transporting SQL PLAN Baselines from one database to another. (Doc ID 880485.1)
SQL> show parameteroptimizer_capture_sql_plan_baselines
NAME TYPE VALUE
---------------------------------------------------------- ------------------------------
optimizer_capture_sql_plan_baselinesboolean FALSE
SQL> alter system set optimizer_capture_sql_plan_baselines=true;
System altered.
SQL> show parameter optimizer_capture_sql_plan_baselines
NAME TYPE VALUE
---------------------------------------------------------- ------------------------------
optimizer_capture_sql_plan_baselinesboolean TRUE
SQL>
SQL> col plan_name for a35
SQL> col sql_handle for a30
SQL> col origin for a15
SQL> selectSQL_HANDLE,plan_name,origin,enabled,accepted,fixed from DBA_SQL_PLAN_BASELINES;
--获取刚才查询的SQL_ID:
SQL> SELECT sql_id
2 FROM v$sql
3 WHERE sql_text LIKE '%spm_test_tab%'
4 AND sql_text NOT LIKE'%dba_sql_plan_baselines%'
5 AND sql_text NOT LIKE '%EXPLAIN%';
SQL_ID
--------------------------
gat6z1bc6nc2d
--使用SQL_ID 从cursorcache中手工捕获执行计划:
SET SERVEROUTPUT ON
DECLARE
l_plans_loaded PLS_INTEGER;
BEGIN
l_plans_loaded := DBMS_SPM.load_plans_from_cursor_cache(
sql_id => 'gat6z1bc6nc2d');
--使用DBA_SQL_PLAN_BASELINES视图查看SPM 信息:
SQL> col sql_handle for a35
SQL> col plan_name for a35
SQL> set lin 120
SQL> SELECT sql_handle, plan_name,enabled, accepted
2 FROM dba_sql_plan_baselines
3 WHERE sql_text LIKE '%spm_test_tab%'
4 AND sql_text NOT LIKE'%dba_sql_plan_baselines%';
Plan:SQL_PLAN_7qxjk7bch8h5ted3324c0
------------------------------------
Plan was verified: Time used .13 seconds.
Plan passed performance criterion: 15.01 times better than baselineplan.
Plan was changed to an accepted plan.
Baseline Plan Test Plan Stats Ratio
------------- --------- -----------
Execution Status: COMPLETE COMPLETE
DBMS_SPM.EVOLVE_SQL_PLAN_BASELINE(SQL_HANDLE=>'SQL_7B76323AD90440B9')
--------------------------------------------------------------------------------
Report Summary
-------------------------------------------------------------------------------
Number of plans verified: 1
Number of plans accepted: 1
SQL>
--再次查看DBA_SQL_PLAN_BASELINES视图:
SQL> SELECT sql_handle, plan_name,enabled, accepted
2 FROM dba_sql_plan_baselines
3 WHERE sql_handle = 'SQL_7b76323ad90440b9';
2.5 修改 Plan Baselines
通过ALTER_SQL_PLAN_BASELINE函数可以修改执行计划的属性,具体修改的选项如下:
(1) enabled (YES/NO) : If YES, the plan is available for theoptimizer if it is also marked as accepted.
(2) fixed (YES/NO) : If YES, the SQL plan baseline will not evolveover time. Fixed plans are used in preference to non-fixed plans.
(3) autopurge (YES/NO) : If YES, the SQL plan baseline is purgedautomatically if it is not used for a period of time.
(4) plan_name : Used to amend the SQL plan name, up to a maximum of30 character.
(5) description : Used to amend the SQL plan description, up to amaximum of 30 character.
--验证:
SQL> set lin 180
SQL> col sql_handle for a25
SQL> col plan_name for a35
SQL> col origin for a15
SQL> selectSQL_HANDLE,plan_name,origin,enabled,accepted,fixed from DBA_SQL_PLAN_BASELINES;
SQL_HANDLE PLAN_NAME ORIGIN ENABLE ACCEPT FIXED
------------------------------------------------------------ --------------- ------ ------ ------
SQL_267afeb2e8216c2d SQL_PLAN_2cyryqbn22v1da82c8876 MANUAL-LOAD YES YES NO
SQL_496b0b4abd8a2948 SQL_PLAN_4kusb9aysnaa865d30abd AUTO-CAPTURE YES YES NO
SQL_5f081cda0133e385 SQL_PLAN_5y20wv80m7sw5391601ca AUTO-CAPTURE YES YES NO
SQL_7b76323ad90440b9 SQL_PLAN_7qxjk7bch8h5tb65c37c8 MANUAL-LOAD YES YES YES
SQL_7b76323ad90440b9 SQL_PLAN_7qxjk7bch8h5ted3324c0 AUTO-CAPTURE YES YES NO
SQL_a6f4c0adedb52ad0 SQL_PLAN_adx60prqvaaqhf8e55c8a AUTO-CAPTURE YES YES NO
SQL_f88491799a8f900b SQL_PLAN_gj14jg6d8z40ba052f708 AUTO-CAPTURE YES YES NO
--再次查看我们之前的SQL:
SQL> SELECT description
2 FROM spm_test_tab
3 WHERE id = 99;
Execution Plan
----------------------------------------------------------
Plan hash value: 1107868462
--------------------------------------------------------------------------------
SQL handle: SQL_7b76323ad90440b9
SQL text: SELECT description FROM spm_test_tab WHERE id = 99
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Plan name:SQL_PLAN_7qxjk7bch8h5ted3324c0 Plan id: 3979551936
Enabled: YES Fixed: NO Accepted: YES Origin: AUTO-CAPTURE
--------------------------------------------------------------------------------
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------
Plan hash value: 3121206333
2.7 设置SQL Management Base SQL Management Plan的数据存在在SYSAUX表空间下面,存放的数据包括SQL Plan baselines,statement logs,plan histories 和SQL Profiles。 SMB 能分配的磁盘空间由如下2个属性控制。 可以使用DBMS_SPM.CONFIGURE过程来进行修改:
(1) space_budget_percent (default10) : Maximum size as a percentage of SYSAUX space.Allowable values 1-50.
(2) plan_retention_weeks (default 53) : Number of weeks unusedplans are retained before being purged. Allowable values 5-523 weeks.
SQL> col parameter_name for a25
SQL> SELECT parameter_name, parameter_valueFROM dba_sql_management_config;
2.8.2 将SQL Planbaselines 导入staging table 使用PACK_STGTAB_BASELINE函数可以实现这个功能,该函数有一些参数,具体参考官方文档。 这里示例将所有SQL Plan Baselines导入staging table。
SET SERVEROUTPUT ON
DECLARE
l_plans_packed PLS_INTEGER;
BEGIN
l_plans_packed := DBMS_SPM.pack_stgtab_baseline(
table_name =>'spm_stageing_tab',
table_owner => 'DAVE');