暂无图片
暂无图片
暂无图片

探索理解spm和sql profiles 关系?优先级? -m6米乐安卓版下载

原创 心在梦在²º²º 2022-07-22
875

探索spm和sql profiles

生产中偶尔会碰到一些sql,有多种执行计划,其中部分情况是统计信息过旧造成的,重新收集下统计信息就行了。
但是有些时候重新收集统计信息也解决不了问题,oracle提供了几种方式来绑定执行计划,帮我我们解决执行计划错误的问题。

oracle优化器辅助手段发展史:

oracle 8:hint oracle 8i&9: stored outline oracle 10: sql profile oracle 11: sql plan manangement

      oracle 11g之后推出spm(sql plan management),spm是一种主动稳定执行计划的手段,能够保证只有被验证过的执行计划才会被启用。spm默认开启,但是自动捕获默认是关闭的。

sql> show parameter sql_plan name type value ------------------------------------ ---------------------- ------------------------------ optimizer_capture_sql_plan_baselines boolean false optimizer_use_sql_plan_baselines boolean true

oracle spm 主要组件:

  • plan capture 捕获
    存储sql对应plan的相关信息。这些信息可以唯一确定一个plan。

  • plan selection 选择
    在系统的运行时,oracle要保证每次执行sql的执行计划都是使用sql baseline中的确定执行计划。同时,跟踪所有该statement执行中生成的新执行计划,作为plan histroy信息保存下来。

  • plan evolution 进化
    添加新的plan到sql baselines中,自动或手动。
    通过使用baseline,减少性能回退。可以为新应用生成baselines,逐步演化更好的plan。

1. 构造环境

sql> create table t (id int,name varchar2(20)); table created. sql> insert into t values (1,'a1'); 1 row created. sql> insert into t values (2,'a2'); 1 row created. sql> insert into t values (3,'a3'); 1 row created. sql> insert into t values (4,'a4'); 1 row created. sql> insert into t values (5,'a5'); 1 row created. sql> commit; commit complete.

2. 执行sql语句,查看执行计划

sql> set autotrace off sql> set line222 sql> alter session set statistics_level=all ; session altered. sql> select * from t where id =1; id name ---------- ---------------------------------------- 1 a1 sql> select * from table(dbms_xplan.display_cursor(null,null,'allstats last')); plan_table_output ------------------------------------------------------------------------------------------------------------ sql_id 85yjka0hqww9x, child number 0 ------------------------------------- select * from t where id =1 plan hash value: 1601196873 ------------------------------------------------------------------------------------ | id | operation | name | starts | e-rows | a-rows | a-time | buffers | ------------------------------------------------------------------------------------ | 0 | select statement | | 1 | | 1 |00:00:00.01 | 8 | |* 1 | table access full| t | 1 | 1 | 1 |00:00:00.01 | 8 | plan_table_output ------------------------------------------------------------------------------------------------------------ predicate information (identified by operation id): --------------------------------------------------- 1 - filter("id"=1) note ----- - dynamic sampling used for this statement (level=2) 22 rows selected.

 结论:全表扫描。 

3. 创建sql plan baseline

-- 通过包dbms_spm.load_plans_from_cursor_cache函数为一条已经在游标缓存中的语句创建基线 sql> declare 2 l_plans_loaded pls_integer; 3 begin 4 l_plans_loaded := dbms_spm.load_plans_from_cursor_cache(sql_id => '85yjka0hqww9x',plan_hash_value=>null); 5 end; 6 / pl/sql procedure successfully completed.

4. 查看基准线信息

sql> col sql_text for a50 sql> col sql_handle for a25 sql> col plan_name for a30 sql> select sql_handle,plan_name,sql_text,accepted from dba_sql_plan_baselines; sql_handle plan_name sql_text accept ------------------------- ------------------------------ ------------------------------------------- sql_57aaf60b37d40f54 sql_plan_5garq1cvx83un94ecae5c select * from t where id =1 yes

5. 通过函数来查看基线的具体执行计划信息

sql> select * from table(dbms_xplan.display_sql_plan_baseline(sql_handle=>'sql_57aaf60b37d40f54',plan_name=>'sql_plan_5garq1cvx83un94ecae5c')); plan_table_output ------------------------------------------------------------------------------------------------------------ sql handle: sql_57aaf60b37d40f54 sql text: select * from t where id =1 -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- plan name: sql_plan_5garq1cvx83un94ecae5c plan id: 2498539100 enabled: yes fixed: no accepted: yes origin: manual-load -------------------------------------------------------------------------------- plan_table_output ------------------------------------------------------------------------------------------------------------ plan hash value: 1601196873 -------------------------------------------------------------------------- | id | operation | name | rows | bytes | cost (%cpu)| time | -------------------------------------------------------------------------- | 0 | select statement | | 1 | 25 | 2 (0)| 00:00:01 | |* 1 | table access full| t | 1 | 25 | 2 (0)| 00:00:01 | -------------------------------------------------------------------------- predicate information (identified by operation id): --------------------------------------------------- 1 - filter("id"=1) 24 rows selected.

6. 创建索引

sql> create index t_idx1 on t(id); index created.

7. 再次执行sql,查看执行计划

sql> set autotrace off sql> set line222 sql> alter session set statistics_level=all ; session altered. sql> select * from t where id =1; id name ---------- ---------------------------------------- 1 a1 sql> select * from table(dbms_xplan.display_cursor(null,null,'allstats last')); plan_table_output ------------------------------------------------------------------------------------------------------------ sql_id 85yjka0hqww9x, child number 2 ------------------------------------- select * from t where id =1 plan hash value: 1601196873 ------------------------------------------------------------------------------------ | id | operation | name | starts | e-rows | a-rows | a-time | buffers | ------------------------------------------------------------------------------------ | 0 | select statement | | 1 | | 1 |00:00:00.01 | 8 | |* 1 | table access full| t | 1 | 1 | 1 |00:00:00.01 | 8 | plan_table_output ------------------------------------------------------------------------------------------------------------ predicate information (identified by operation id): --------------------------------------------------- 1 - filter("id"=1) note ----- - sql plan baseline sql_plan_5garq1cvx83un94ecae5c used for this statement 22 rows selected.

结论:执行计划仍然为全表扫描,并没有走索引,而且提示sql plan baseline已经被应用。

8. 再次查看基准线信息

sql> col sql_text for a50 sql> col sql_handle for a25 sql> col plan_name for a30 sql> select sql_handle,plan_name,sql_text,accepted from dba_sql_plan_baselines; sql_handle plan_name sql_text accept ------------------------- ------------------------------ --------------------------------- ------ sql_57aaf60b37d40f54 sql_plan_5garq1cvx83un94ecae5c select * from t where id =1 yes sql_57aaf60b37d40f54 sql_plan_5garq1cvx83un90007b4c select * from t where id =1 no

结论:由于新创建了索引,优化器再次评估这个sql发现执行计划相比sql计划基线的效率更高,此时会新生成一个不可接受的sql计划基线,accept状态为no。

9. 查看新生产的sql计划基线的信息

sql> select * from table(dbms_xplan.display_sql_plan_baseline(sql_handle=>'sql_57aaf60b37d40f54',plan_name=>'sql_plan_5garq1cvx83un90007b4c')); plan_table_output ------------------------------------------------------------------------------------------------------------ -------------------------------------------------------------------------------- sql handle: sql_57aaf60b37d40f54 sql text: select * from t where id =1 -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- plan name: sql_plan_5garq1cvx83un90007b4c plan id: 2415950668 enabled: yes fixed: no accepted: no origin: auto-capture -------------------------------------------------------------------------------- plan_table_output ------------------------------------------------------------------------------------------------------------ plan hash value: 4055207394 -------------------------------------------------------------------------------------- | id | operation | name | rows | bytes | cost (%cpu)| time | -------------------------------------------------------------------------------------- | 0 | select statement | | 1 | 25 | 1 (0)| 00:00:01 | | 1 | table access by index rowid| t | 1 | 25 | 1 (0)| 00:00:01 | |* 2 | index range scan | t_idx1 | 1 | | 1 (0)| 00:00:01 | -------------------------------------------------------------------------------------- predicate information (identified by operation id): 2 - access("id"=1) 25 rows selected.

结论:新产生的执行计划基线走的是我们想要的索引方式。

10. 演化基线

​ 为了验证基线中一个处于不可接受状态的执行计划是否比一个处于可接受状态的执行计划具有更高的效率,必须通过演化来验证,需要让优化器以不同的执行计划来执行这条sql语句,观察不可接受状态的执行计划基线是否会带来更好的性能,如果性能确实更高,这个不可接受状态的基线将会转换为可接受状态。

简单演示,手工执行:

sql> set serveroutput on sql> set long 10000 sql> declare 2 result_clob clob; 3 begin 4 result_clob:=dbms_spm.evolve_sql_plan_baseline(sql_handle=>'sql_57aaf60b37d40f54',plan_name=>null,verify=>'yes',commit=>'yes'); 5 dbms_output.put_line(result_clob); 6 end; 7 / ------------------------------------------------------------------------------- evolve sql plan baseline report ------------------------------------------------------------------------------- inputs: ------- sql_handle = sql_57aaf60b37d40f54 plan_name = time_limit = dbms_spm.auto_limit verify = yes commit = yes plan: sql_plan_5garq1cvx83un90007b4c ------------------------------------ plan was verified: time used .01 seconds. plan passed performance criterion: 3.5 times better than baseline plan. plan was changed to an accepted plan. baseline plan test plan stats ratio ------------- --------- ----------- execution status: complete complete rows processed: 1 1 elapsed time(ms): .013 .011 1.18 cpu time(ms): .014 .011 1.27 buffer gets: 7 2 3.5 physical read requests: 0 0 physical write requests: 0 0 physical read bytes: 0 0 physical write bytes: 0 0 executions: 1 1 ------------------------------------------------------------------------------- report summary ------------------------------------------------------------------------------- number of plans verified: 1 number of plans accepted: 1 pl/sql procedure successfully completed. -- 关于verify和commit参数: verify如果为yes(默认值)将执行sql语句演化,如果设置为false将不需要演化; commit如果为yes(默认值)数据字典将按照演化的结果进行修改,如果设置为no,参数verify设置为yes,只进行演化但是不会修改数据字典。

11. 再次查看基准线信息

sql> select sql_handle,plan_name,sql_text,accepted from dba_sql_plan_baselines; sql_handle plan_name sql_text accept ------------------------- ------------------------------ ----------------------------------- ------ sql_57aaf60b37d40f54 sql_plan_5garq1cvx83un94ecae5c select * from t where id =1 yes sql_57aaf60b37d40f54 sql_plan_5garq1cvx83un90007b4c select * from t where id =1 yes

结论:新产生的执行计划基线accept状态也变为yes。

12. 再次执行sql,查看执行计划

sql> set autotrace off sql> set line222 sql> alter session set statistics_level=all ; session altered. sql> select * from t where id =1; id name ---------- ---------------------------------------- 1 a1 sql> select * from table(dbms_xplan.display_cursor(null,null,'allstats last')); plan_table_output ------------------------------------------------------------------------------------------------------------ sql_id 85yjka0hqww9x, child number 2 ------------------------------------- select * from t where id =1 plan hash value: 4055207394 ------------------------------------------------------------------------------------------------ | id | operation | name | starts | e-rows | a-rows | a-time | buffers | ------------------------------------------------------------------------------------------------ | 0 | select statement | | 1 | | 1 |00:00:00.01 | 3 | | 1 | table access by index rowid| t | 1 | 1 | 1 |00:00:00.01 | 3 | plan_table_output ------------------------------------------------------------------------------------------------------------ |* 2 | index range scan | t_idx1 | 1 | 1 | 1 |00:00:00.01 | 2 | ------------------------------------------------------------------------------------------------ predicate information (identified by operation id): --------------------------------------------------- 2 - access("id"=1) note ----- - dynamic sampling used for this statement (level=2) plan_table_output ------------------------------------------------------------------------------------------------------------ - sql plan baseline sql_plan_5garq1cvx83un90007b4c used for this statement 24 rows selected

结论:执行计划变为全表扫描,sql plan baseline变为新产生的基线。  

思考: 如果一个sql中,即存在sql plan baseline,也同时存在sql profile,那么执行计划是怎么样的呢?

看到有文档是说:profile优先级大于baseline,如果有profile存在,优先使用profile。那么这个说法准确吗?

 查阅官方文档描述:sql profile 和 sql plan baseline 之间,没有严格的关系。

通过实验验证:

实验1:

基于上面的实验继续做,此时sql语句走的是索引扫描,sql plan baseline为:sql_plan_5garq1cvx83un90007b4c

1. 使用sql_profile绑定

-- 通过coe_load_sql_profile.sql 绑定了全表扫描的执行计划。过程省略..... sql> select name,sql_text from dba_sql_profiles; name sql_text ------------------------------------------------------------ ----------------------------------- 85yjka0hqww9x_1601196873 select * from t where id =1

2. 再次执行sql,查看执行计划

sql> set autotrace off sql> set line222 sql> alter session set statistics_level=all ; session altered. sql> select * from t where id =1; id name ---------- ---------------------------------------- 1 a1 sql> select * from table(dbms_xplan.display_cursor(null,null,'allstats last')); plan_table_output ------------------------------------------------------------------------------------------------------------ sql_id 85yjka0hqww9x, child number 1 ------------------------------------- select * from t where id =1 plan hash value: 4055207394 ------------------------------------------------------------------------------------------------ | id | operation | name | starts | e-rows | a-rows | a-time | buffers | ------------------------------------------------------------------------------------------------ | 0 | select statement | | 1 | | 1 |00:00:00.01 | 3 | | 1 | table access by index rowid| t | 1 | 1 | 1 |00:00:00.01 | 3 | plan_table_output ------------------------------------------------------------------------------------------------------------ |* 2 | index range scan | t_idx1 | 1 | 1 | 1 |00:00:00.01 | 2 | ------------------------------------------------------------------------------------------------ predicate information (identified by operation id): --------------------------------------------------- 2 - access("id"=1) note ----- - sql profile 85yjka0hqww9x_1601196873 used for this statement - sql plan baseline sql_plan_5garq1cvx83un90007b4c used for this statement 24 rows selected.

结论:我们可以看到sql profile和sql plan baseline两者都应用到了执行计划中,但是走的是index range scan扫描方式,并没有按照sql profile中走全表扫描的方式。

3. 10053 trace分析

-- 10053 trace文件用来查看oracle如何根据成本选择执行计划 sql> alter session set events '10053 trace name context forever, level 12'; sql> select * from t where id =1; sql> alter session set events '10053 trace name context off';

– 查看trace 内容:

-- 1) full的hint先被应用,sql profile生效了 final query after transformations:******* unparsed query is ******* select /* full ("t") */ "t"."id" "id","t"."name" "name" from "sxc"."t" "t" where "t"."id"=1 kkoqbc: optimizing query block sel$1 (#0) : call(in-use=1280, alloc=16344), compile(in-use=64584, alloc=66928), execution(in-use=3712, alloc=4032) kkoqbc-subheap (create addr=0x7fdca254fa10) **************** query block text **************** select * from t where id =1 --------------------- query block signature --------------------- signature (optimizer): qb_name=sel$1 nbfros=1 flg=0 fro(0): flg=0 objn=93952 hint_alias="t"@"sel$1" ----------------------------- system statistics information ----------------------------- using noworkload stats cpuspeednw: 3074 millions instructions/sec (default is 100) iotfrspeed: 4096 bytes per millisecond (default is 4096) ioseektim: 10 milliseconds (default is 10) mbrc: no value blocks (default is 8) *************************************** base statistical information *********************** table stats:: table: t alias: t (not analyzed) #rows: 409 #blks: 5 avgrowlen: 100.00 chaincnt: 0.00 index stats:: index: t_idx1 col#: 1 lvls: 0 #lb: 1 #dk: 5 lb/k: 1.00 db/k: 1.00 cluf: 1.00 access path analysis for t *************************************** single table access path single table cardinality estimation for t[t] column (#1): id( no statistics (using defaults) avglen: 13 ndv: 13 nulls: 0 density: 0.078240 table: t alias: t card: original: 409.000000 rounded: 4 computed: 4.09 non adjusted: 4.09 access path: tablescan cost: 3.00 resp: 3.00 degree: 0 cost_io: 3.00 cost_cpu: 117487 resp_io: 3.00 resp_cpu: 117487 best:: accesspath: tablescan -- 全表扫描方式 cost: 3.00 degree: 1 resp: 3.00 card: 4.09 bytes: 0 .......... ......... -- 2)index的hint也被应用,baseline也生效了 final query after transformations:******* unparsed query is ******* select /* full ("t") index_rs_asc ("t" "t_idx1") */ "t"."id" "id","t"."name" "name" from "sxc"."t" "t" where "t"."id"=1 kkoqbc: optimizing query block sel$1 (#0) : call(in-use=1312, alloc=16344), compile(in-use=71104, alloc=72240), execution(in-use=4816, alloc=8088) kkoqbc-subheap (create addr=0x7fdca24eea58) **************** query block text **************** select * from t where id =1 --------------------- query block signature --------------------- signature (optimizer): qb_name=sel$1 nbfros=1 flg=0 fro(0): flg=0 objn=93952 hint_alias="t"@"sel$1" ----------------------------- system statistics information ----------------------------- using noworkload stats cpuspeednw: 3074 millions instructions/sec (default is 100) iotfrspeed: 4096 bytes per millisecond (default is 4096) ioseektim: 10 milliseconds (default is 10) mbrc: no value blocks (default is 8) *************************************** base statistical information *********************** table stats:: table: t alias: t (not analyzed) #rows: 409 #blks: 5 avgrowlen: 100.00 chaincnt: 0.00 index stats:: index: t_idx1 col#: 1 lvls: 0 #lb: 1 #dk: 5 lb/k: 1.00 db/k: 1.00 cluf: 1.00 access path analysis for t *************************************** single table access path single table cardinality estimation for t[t] column (#1): id( no statistics (using defaults) avglen: 13 ndv: 13 nulls: 0 density: 0.078240 table: t alias: t card: original: 409.000000 rounded: 4 computed: 4.09 non adjusted: 4.09 access path: tablescan cost: 3.00 resp: 3.00 degree: 0 cost_io: 3.00 cost_cpu: 117487 resp_io: 3.00 resp_cpu: 117487 access path: index (alleqguess) index: t_idx1 resc_io: 2.00 resc_cpu: 14613 ix_sel: 0.004000 ix_sel_with_filters: 0.004000 cost: 2.00 resp: 2.00 degree: 1 best:: accesspath: indexrange --索引扫描方式 index: t_idx1 cost: 2.00 degree: 1 resp: 2.00 card: 4.09 bytes: 0

结论:

1. sql profile和spm baseline是一起作用,hint中会被合并,本案例中,被合并为 /* full (“t”) index_rs_asc (“t” “t_idx1”) */

2. 当2个hint是冲突的时候,oracle 会忽略互相冲突的提示hint的组合。

4. 删除baseline

sql> set serveroutput on sql> declare 2 v_text pls_integer; 3 begin 4 v_text := dbms_spm.drop_sql_plan_baseline(sql_handle => 'sql_57aaf60b37d40f54',plan_name => null); 5 dbms_output.put_line(v_text); 6 end; 7 / 2 pl/sql procedure successfully completed. sql> select sql_handle,plan_name,sql_text,accepted from dba_sql_plan_baselines; no rows selected

5. 再次执行sql,查看执行计划

sql> set autotrace off sql> set line222 alter session set statistics_level=all ; session altered. select * from t where id =1; id name ---------- ---------------------------------------- 1 a1 sql> select * from table(dbms_xplan.display_cursor(null,null,'allstats last')); plan_table_output ------------------------------------------------------------------------------------------------------------ sql_id 85yjka0hqww9x, child number 0 ------------------------------------- select * from t where id =1 plan hash value: 1601196873 ------------------------------------------------------------------------------------ | id | operation | name | starts | e-rows | a-rows | a-time | buffers | ------------------------------------------------------------------------------------ | 0 | select statement | | 1 | | 1 |00:00:00.01 | 8 | |* 1 | table access full| t | 1 | 1 | 1 |00:00:00.01 | 8 | plan_table_output ------------------------------------------------------------------------------------------------------------ predicate information (identified by operation id): --------------------------------------------------- 1 - filter("id"=1) note ----- - sql profile 85yjka0hqww9x_1601196873 used for this statement 22 rows selected.

结论:只剩下sql profile 被应用,且执行计划为全表扫描。

实验2:

 清空所有数据,重新开始实验。

1. 清空之前测试数据

-- 1.删除测试表 sql> drop table t; table dropped. -- 2.删除sql_profile sql> exec dbms_sqltune.drop_sql_profile(name => '85yjka0hqww9x_1601196873'); pl/sql procedure successfully completed. -- 3.删除sql plan baselines sql> set serveroutput on sql> declare 2 v_text pls_integer; 3 begin 4 v_text := dbms_spm.drop_sql_plan_baseline(sql_handle => 'sql_57aaf60b37d40f54',plan_name => null); 5 dbms_output.put_line(v_text); 6 end; 7 / 2 pl/sql procedure successfully completed. sql> select count(*) from dba_sql_profiles; count(*) ---------- 0 sql> select count(*) from dba_sql_plan_baselines; count(*) ---------- 0

2. 重新构造环境

sql> create table t (id int,name varchar2(20)); table created. sql> insert into t values (1,'a1'); 1 row created. sql> insert into t values (2,'a2'); 1 row created. sql> insert into t values (3,'a3'); 1 row created. sql> insert into t values (4,'a4'); 1 row created. sql> insert into t values (5,'a5'); 1 row created. sql> commit; commit complete.

3. 执行sql语句,查看执行计划

sql> set autotrace off sql> set line222 sql> alter session set statistics_level=all ; session altered. sql> select * from t where id =1; id name ---------- ---------------------------------------- 1 a1 sql> select * from table(dbms_xplan.display_cursor(null,null,'allstats last')); plan_table_output ------------------------------------------------------------------------------------------------------------ sql_id 85yjka0hqww9x, child number 1 ------------------------------------- select * from t where id =1 plan hash value: 1601196873 ------------------------------------------------------------------------------------ | id | operation | name | starts | e-rows | a-rows | a-time | buffers | ------------------------------------------------------------------------------------ | 0 | select statement | | 1 | | 1 |00:00:00.01 | 8 | |* 1 | table access full| t | 1 | 1 | 1 |00:00:00.01 | 8 | plan_table_output ------------------------------------------------------------------------------------------------------------ predicate information (identified by operation id): --------------------------------------------------- 1 - filter("id"=1) note ----- - dynamic sampling used for this statement (level=2) 22 rows selected.

结论:全表扫描。

4. 创建sql plan baseline

-- 通过包dbms_spm.load_plans_from_cursor_cache函数为一条已经在游标缓存中的语句创建基线 sql> declare 2 l_plans_loaded pls_integer; 3 begin 4 l_plans_loaded := dbms_spm.load_plans_from_cursor_cache(sql_id => '85yjka0hqww9x',plan_hash_value=>null); 5 end; 6 / pl/sql procedure successfully completed.

5. 查看基准线信息

sql> col sql_text for a50 sql> col sql_handle for a25 sql> col plan_name for a30 sql> select sql_handle,plan_name,sql_text,accepted from dba_sql_plan_baselines; sql_handle plan_name sql_text accept ------------------------- ------------------------------ ---------------------------------- ------ sql_57aaf60b37d40f54 sql_plan_5garq1cvx83un94ecae5c select * from t where id =1 yes

6. 创建索引

sql> create index t_idx1 on t(id); index created

7. 再次执行sql,查看执行计划

sql>set autotrace off sql>set line222 sql>alter session set statistics_level=all ; session altered. sql>select * from t where id =1; id name ---------- ---------------------------------------- 1 a1 sql>select * from table(dbms_xplan.display_cursor(null,null,'allstats last')); plan_table_output ------------------------------------------------------------------------------------------------------------ sql_id 85yjka0hqww9x, child number 1 ------------------------------------- select * from t where id =1 plan hash value: 1601196873 ------------------------------------------------------------------------------------ | id | operation | name | starts | e-rows | a-rows | a-time | buffers | ------------------------------------------------------------------------------------ | 0 | select statement | | 1 | | 1 |00:00:00.01 | 8 | |* 1 | table access full| t | 1 | 1 | 1 |00:00:00.01 | 8 | plan_table_output ------------------------------------------------------------------------------------------------------------ predicate information (identified by operation id): --------------------------------------------------- 1 - filter("id"=1) note ----- - sql plan baseline sql_plan_5garq1cvx83un94ecae5c used for this statement 22 rows selected.

结论:仍然全表扫描,并没有走索引,和前面的测试结果一样,符合预期。

8. 查看基准线信息

sql> select sql_handle,plan_name,sql_text,accepted from dba_sql_plan_baselines; sql_handle plan_name sql_text accept ------------------------- ------------------------------ --------------------------------- ------ sql_57aaf60b37d40f54 sql_plan_5garq1cvx83un94ecae5c select * from t where id =1 yes sql_57aaf60b37d40f54 sql_plan_5garq1cvx83un90007b4c select * from t where id =1 no

结论:也生产了新的基线,accept状态为no。

9. 使用sql profile绑定

-- 通过coe_load_sql_profile.sql 绑定了全表扫描的执行计划。过程省略..... sql> @coe_load_sql_profile ... coe_load_sql_profile completed.

10. 演化基线

sql> set serveroutput on sql> set long 10000 sql> declare 2 result_clob clob; 3 begin 4 result_clob:=dbms_spm.evolve_sql_plan_baseline(sql_handle=>'sql_57aaf60b37d40f54',plan_name=>null,verify=>'yes',commit=>'yes'); 5 dbms_output.put_line(result_clob); 6 end; 7 / ------------------------------------------------------------------------------- evolve sql plan baseline report ------------------------------------------------------------------------- inputs: ------- sql_handle = sql_57aaf60b37d40f54 plan_name = time_limit = dbms_spm.auto_limit verify = yes commit = yes plan: sql_plan_5garq1cvx83un90007b4c ------------------------------------ plan was verified: time used .01 seconds. plan passed performance criterion: 3.5 times better than baseline plan. plan was changed to an accepted plan. baseline plan test plan stats ratio ------------- --------- ----------- execution status: complete complete rows processed: 1 1 elapsed time(ms): .023 .01 2.3 cpu time(ms): .023 .01 2.3 buffer gets: 7 2 3.5 physical read requests: 0 0 physical writerequests: 0 0 physical read bytes: 0 0 physical write bytes: 0 0 executions: 1 1 ------------------------------------------------------------------------------- report summary ------------------------------------------------------------------------------- number of plans verified: 1 number of plans accepted: 1 pl/sql procedure successfully completed.

11. 查看基准线信息

sql> select sql_handle,plan_name,sql_text,accepted from dba_sql_plan_baselines; sql_handle plan_name sql_text accept ------------------------- ------------------------------ ---------------------------------- ------ sql_57aaf60b37d40f54 sql_plan_5garq1cvx83un94ecae5c select * from t where id =1 yes sql_57aaf60b37d40f54 sql_plan_5garq1cvx83un90007b4c select * from t where id =1 yes

结论:演化后,新产生的执行计划基线accept状态也变为yes。

12 .再次执行sql,查看执行计划

sql> set autotrace off sql> set line222 sql> alter session set statistics_level=all ; session altered. sql> select * from t where id =1; id name ---------- ---------------------------------------- 1 a1 sql> select * from table(dbms_xplan.display_cursor(null,null,'allstats last')); plan_table_output ------------------------------------------------------------------------------------------------------------ sql_id 85yjka0hqww9x, child number 0 ------------------------------------- select * from t where id =1 plan hash value: 1601196873 ------------------------------------------------------------------------------------ | id | operation | name | starts | e-rows | a-rows | a-time | buffers | ------------------------------------------------------------------------------------ | 0 | select statement | | 1 | | 1 |00:00:00.01 | 8 | |* 1 | table access full| t | 1 | 1 | 1 |00:00:00.01 | 8 | plan_table_output ------------------------------------------------------------------------------------------------------------ predicate information (identified by operation id): --------------------------------------------------- 1 - filter("id"=1) note ----- - sql profile 85yjka0hqww9x_1601196873 used for this statement - sql plan baseline sql_plan_5garq1cvx83un94ecae5c used for this statement 23 rows selected.

结论:sql profil 和 sql plan baseline 都被应用,但是执行计划变为仍然是全表扫描,sql plan baseline仍然是老的基线sql_plan_5garq1cvx83un94ecae5c,并没有采用新的基线。

13. 删除sql profile

sql> exec dbms_sqltune.drop_sql_profile(name => '85yjka0hqww9x_1601196873'); pl/sql procedure successfully completed.

14 .查看执行计划

sql> set autotrace off sql> set line222 sql> alter session set statistics_level=all ; session altered. sql> select * from t where id =1; id name ---------- ---------------------------------------- 1 a1 sql> select * from table(dbms_xplan.display_cursor(null,null,'allstats last')); plan_table_output ------------------------------------------------------------------------------------------------------------ sql_id 85yjka0hqww9x, child number 0 ------------------------------------- select * from t where id =1 plan hash value: 4055207394 ------------------------------------------------------------------------------------------------ | id | operation | name | starts | e-rows | a-rows | a-time | buffers | ------------------------------------------------------------------------------------------------ | 0 | select statement | | 1 | | 1 |00:00:00.01 | 3 | | 1 | table access by index rowid| t | 1 | 1 | 1 |00:00:00.01 | 3 | plan_table_output ------------------------------------------------------------------------------------------------------------ |* 2 | index range scan | t_idx1 | 1 | 1 | 1 |00:00:00.01 | 2 | ------------------------------------------------------------------------------------------------ predicate information (identified by operation id): --------------------------------------------------- 2 - access("id"=1) note ----- - dynamic sampling used for this statement (level=2) plan_table_output ------------------------------------------------------------------------------------------------------------ - sql plan baseline sql_plan_5garq1cvx83un90007b4c used for this statement 24 rows selected.

结论:删除sql profile后,只有sql plan baseline被应用,而且执行计划也变为index扫描,sql plan baseline变成了新的基线sql_plan_5garq1cvx83un90007b4c。

15. 再次使用sql profile绑定

-- 再次通过coe_load_sql_profile.sql 绑定了全表扫描的执行计划。过程省略..... sql> @coe_load_sql_profile ... coe_load_sql_profile completed.

16. 查看执行计划

sql> set autotrace off sql> set line222 sql> alter session set statistics_level=all ; session altered. sql> select * from t where id =1; id name ---------- ---------------------------------------- 1 a1 sql> select * from table(dbms_xplan.display_cursor(null,null,'allstats last')); plan_table_output ------------------------------------------------------------------------------------------------------------ sql_id 85yjka0hqww9x, child number 0 ------------------------------------- select * from t where id =1 plan hash value: 1601196873 ------------------------------------------------------------------------------------ | id | operation | name | starts | e-rows | a-rows | a-time | buffers | ------------------------------------------------------------------------------------ | 0 | select statement | | 1 | | 1 |00:00:00.01 | 8 | |* 1 | table access full| t | 1 | 1 | 1 |00:00:00.01 | 8 | plan_table_output ------------------------------------------------------------------------------------------------------------ predicate information (identified by operation id): --------------------------------------------------- 1 - filter("id"=1) note ----- - sql profile 85yjka0hqww9x_1601196873 used for this statement - sql plan baseline sql_plan_5garq1cvx83un94ecae5c used for this statement 23 rows selected.

结论:sql profil 和 sql plan baseline 又都被应用,但是执行计划又变为全表扫描,sql plan baseline又变成老的基线sql_plan_5garq1cvx83un94ecae5c。

17. 删除全表扫描的sql plan baseline

sql>declare 2 v_text pls_integer; 3 begin 4 v_text := dbms_spm.drop_sql_plan_baseline(sql_handle => 'sql_57aaf60b37d40f54',plan_name => 'sql_plan_5garq1cvx83un94ecae5c'); 5 dbms_output.put_line(v_text); 6 end; 7 / pl/sql procedure successfully completed. sql>select sql_handle,plan_name,sql_text,accepted from dba_sql_plan_baselines; sql_handle plan_name sql_text accept ------------------------- ------------------------------ ----------------------------------------- sql_57aaf60b37d40f54 sql_plan_5garq1cvx83un90007b4c select * from t where id =1 yes

结论:只剩下一个新的基线,对应的执行计划为index扫描。

18. 再次执行sql,查看执行计划

sql> set autotrace off sql> set line222 sql> alter session set statistics_level=all ; session altered. sql> select * from t where id =1; id name ---------- ---------------------------------------- 1 a1 sql> select * from table(dbms_xplan.display_cursor(null,null,'allstats last')); plan_table_output ------------------------------------------------------------------------------------------------------------ sql_id 85yjka0hqww9x, child number 1 ------------------------------------- select * from t where id =1 plan hash value: 4055207394 ------------------------------------------------------------------------------------------------ | id | operation | name | starts | e-rows | a-rows | a-time | buffers | ------------------------------------------------------------------------------------------------ | 0 | select statement | | 1 | | 1 |00:00:00.01 | 3 | | 1 | table access by index rowid| t | 1 | 1 | 1 |00:00:00.01 | 3 | plan_table_output ------------------------------------------------------------------------------------------------------------ |* 2 | index range scan | t_idx1 | 1 | 1 | 1 |00:00:00.01 | 2 | ------------------------------------------------------------------------------------------------ predicate information (identified by operation id): --------------------------------------------------- 2 - access("id"=1) note ----- - sql profile 85yjka0hqww9x_1601196873 used for this statement - sql plan baseline sql_plan_5garq1cvx83un90007b4c used for this statement 24 rows selected.

结论:删除全表扫描的基线后,sql profil 和 sql plan baseline 仍然都被应用,但是执行计划变为index扫描,sql plan baseline变为新的基线sql_plan_5garq1cvx83un90007b4c。 

最后修改时间:2022-07-22 10:43:52
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【米乐app官网下载的版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论

网站地图