SQL PROFILE:相对被动
SPM:主动,保证只有被验证过的执行计划才会被启用。只有DBA_SQL_PLAN_BASELINES中ENABLED和ACCEPTED值均为YES的SQLPLAN BASELINE对应的执行计划才会被ORACLE启用。
自动捕获:
对于重复执行的SQL,ORACLE会自动捕获其SQLPLAN BASELINE.
11gR2以上环境,ACCEPTED 不能被设为NO!只能改ENABLED的值。
需要联合使用dbms_spm.evolve_sql_plan_baseline与dbms_spm.alter_sql_plan_baseline来启用新的执行计划。
手动生成SQL PLAN BASELINE:
1. 生成原目标对应的SQL PLAN BASELINE
2. 加对加HINT的新的SQL生成SQLPLAN BASELINE,不过使用原来的SQL_HANDLE
3. DROP 旧的SQL PLAN BASELINE
自动捕获:
show parameter sql_plan;
alter session set optimizer_use_sql_plan_baselines =FALSE;
alter session set optimizer_capture_sql_plan_baselines=TRUE;
create table T52 as select * from dba_objects;
create index idx_t52 on T52(object_id);
execdbms_stats.gather_table_stats(ownname=>'TESTER',tabname=>'T52',cascade=>true);
select object_id,object_name from T52 where object_idbetween 103 and 108;
select sql_handle,plan_name,origin,enabled,accepted,sql_textfrom dba_sql_plan_baselines where sql_text like 'select object_id%';
select object_id,object_name from T52 where object_idbetween 103 and 108;
select sql_handle,plan_name,origin,enabled,accepted,sql_textfrom dba_sql_plan_baselines where sql_text like 'select object_id%';
exec dbms_stats.set_index_stats(ownname =>'TESTER',indname => 'IDX_T52',clstfct => 24000000,no_invalidate =>false);
select index_name,clustering_factor from dba_indexes whereindex_name='IDX_T52';
select object_id,object_name from T52 where object_id between103 and 108;
select sql_handle,plan_name,origin,enabled,accepted,sql_textfrom dba_sql_plan_baselines where sql_text like 'select object_id%';
alter session set optimizer_use_sql_plan_baselines =TRUE;
alter session set optimizer_capture_sql_plan_baselines=FALSE;
select object_id,object_name from T52 where object_idbetween 103 and 108;
var temp varchar2(1000);
exec :temp :=dbms_spm.evolve_sql_plan_baseline(sql_handle=>'SQL_d56b2c2c32dc3903',plan_name=>'SQL_PLAN_dautc5htdsf83b2fbea4c',verify=>'NO',commit=>'YES');---AcceptedNO->YES
exec :temp :=dbms_spm.alter_sql_plan_baseline(sql_handle=>'SQL_d56b2c2c32dc3903',plan_name=>'SQL_PLAN_dautc5htdsf83fe6bb48d',attribute_name=>'ENABLED',attribute_value=>'NO');--ENABLED YES->NO;
手动:
select /*+ no_index(T52 idx_t52)*/ object_name,object_idfrom T52 where object_id =4;
select * from table(dbms_xplan.display_cursor(null,null,'advanced'));
select sql_handle,plan_name,origin,enabled,accepted,sql_textfrom dba_sql_plan_baselines where sql_text like 'select /*+ no_index(T52idx_t52)*/%';
var temp number;
exec :temp :=dbms_spm.load_plans_from_cursor_cache(sql_id=>'7yg3r1g197ryw',plan_hash_value=>1739897948);
select /*+ index(T52 idx_t52)*/ object_name,object_id fromT52 where object_id =4;
select * fromtable(dbms_xplan.display_cursor(null,null,'advanced'));
exec :temp :=dbms_spm.load_plans_from_cursor_cache(sql_id=>'ffxfpta241uu3',plan_hash_value=>3820880511,sql_handle=>'SQL_8178be3c5995ebb7');
select sql_handle,plan_name,origin,enabled,accepted,sql_textfrom dba_sql_plan_baselines where sql_text like 'select /*+ no_index(T52idx_t52)*/%';
exec :temp := dbms_spm.drop_sql_plan_baseline(sql_handle=>'SQL_8178be3c5995ebb7',plan_name=>'SQL_PLAN_82y5y7jctbuxrb2fbea4c');
select /*+ no_index(T52 idx_t52)*/ object_name,object_idfrom T52 where object_id =4;
select * fromtable(dbms_xplan.display_cursor(null,null,'advanced'));