当前位置: 移动技术网 > IT编程>数据库>Oracle > Oracle中基于hint的3种执行计划控制方法详细介绍

Oracle中基于hint的3种执行计划控制方法详细介绍

2017年12月12日  | 移动技术网IT编程  | 我要评论

侠盗飞车罪恶都市秘籍无限钱,都市良人行好看吗,短篇鬼故事大全

hint(提示)无疑是最基本的控制执行计划的方式了;通过在sql语句中直接嵌入优化器指令,进而使优化器在语句执行时强制的选择hint指定的执行路径,这种使用方式最大的好处便是方便和快捷,定制度也很高,通常在对某些sql语句执行计划进行微调的时候我会首选这种方式,不过尽管如此,hint在使用中仍然有很多不可忽视的问题;

使用hint过程中有一些值得注意的细则,首先便是要准确的识别对应的查询块,如果需要使用注释也可以hint中声明;对于使用别名的对象一律使用别名来引用,并且诸如“用户名.对象”的引用方式也不被允许,这几个都是我平时经常犯的错误,其实细心一点也就没什么关系了,不过最郁闷的是使用hint的过程中没有任何提示信息可以参考!!譬如语句中使用了无效的hint,oracle并不会给予你任何相关的错误信息,相反这些hint会在执行时被默默的忽略,像什么都没发生一样。。

到这里,我并不想讨论如何正确的使用hint,我想说的是在oracle中,仍然有很多可以控制执行计划的机制,11g中,有三种基于优化器hint的执行计划控制方式:

1.outline(大纲)
2.sql profile(概要文件)
3.sql baseline(基线)

这些方式的使用比较hint更加的系统,完备,它们的出现很大程度上提高了hint这种古老的控制方式的实用性。

outline(大纲)

outline的原理是解析sql语句的执行计划,在此过程中确定一套可以有效的强制优化器选择某个执行计划的hints,然后保存这些hints,当下次发生”相同“查询的时候,优化器便会忽略当前的统计信息因素,选用outline中记录的hints来执行查询,达到控制执行计划的目的。

outline的创建通常有两种方式,一种使用create outline语句,另一种便是借助于专属的dbms_outln包,使用create outline方式时我们需要注明完整查询语句:

复制代码 代码如下:

sql> create outline my_test_outln for category test on
  2  select count(*) from scott.emp;

outline created.

相比之下,dbms_outln.create_outline方式允许通过已经保存在缓存区中的sql语句的hash值来创建outline,因此更加常用,下面是签名:

复制代码 代码如下:

dbms_outln.create_outline (
   hash_value    in number,
   child_number  in number,
   category      in varchar2 default 'default');

category用于指定outline的分类,在一个会话中只能使用一种分类,分类的选择由参数use_stored_outlines决定,该参数的默认值为false,表示不适用outline,设置成true则选用default分类下的outline,如果需要使用非default分类下的outline,可以设置该参数值为对应的分类的名称。

关于outline的视图通常可以查询dba_outlines,dba_outline_hints,数据库中outln用户下也有三张表用于保存outline信息,其中ol#记载了每一个outline的完整定义。

复制代码 代码如下:

sql> select table_name,owner from all_tables where owner='outln';

table_name                     owner
------------------------------ ------------------------------
ol$                            outln
ol$hints                       outln
ol$nodes                       outln

-- 查询当前系统中已有的outline已经对应outline使用的hints:
[sql]
sql> select category,ol_name,hintcount,sql_text from outln.ol$;

category   ol_name                         hintcount sql_text
---------- ------------------------------ ---------- --------------------------------------------------
test       my_test_outln                           6 select count(*) from scott.emp
default    sys_outline_13080517081959001           6 select * from scott.emp where empno=7654

-- 查询对应outline上应用的hints
sql> select name, hint from dba_outline_hints where name = 'sys_outline_13080517081959001';

name                           hint
------------------------------ --------------------------------------------------------------------------------
sys_outline_13080517081959001  index_rs_asc(@"sel$1" "emp"@"sel$1" ("emp"."empno"))
sys_outline_13080517081959001  outline_leaf(@"sel$1")
sys_outline_13080517081959001  all_rows
sys_outline_13080517081959001  db_version('11.2.0.1')
sys_outline_13080517081959001  optimizer_features_enable('11.2.0.1')
sys_outline_13080517081959001  ignore_optim_embedded_hints

6 rows selected.

使用outline来锁定执行计划的完整实例:

复制代码 代码如下:

-- 执行查询
sql> select * from scott.emp where empno=7654;

     empno ename      job              mgr hiredate         sal       comm     deptno
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      7654 martin     salesman        7698 28-sep-81       1250       1400         30

-- 查看该查询的执行计划
-- 注意这里的hash_value和child_number不可作为dbms_outln.create_outline参数值,这些只是plan_table中保存的执行计划的值!!!
sql> select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

plan_table_output
----------------------------------------------------------------------------------------------------
sql_id  40t73tu9dst5y, child number 1
-------------------------------------
select * from scott.emp where empno=7654

plan hash value: 2949544139

------------------------------------------------------------------------------------------------
| id  | operation                   | name   | starts | e-rows | a-rows |   a-time   | buffers |
------------------------------------------------------------------------------------------------
|   0 | select statement            |        |      1 |        |      1 |00:00:00.01 |       2 |
|   1 |  table access by index rowid| emp    |      1 |      1 |      1 |00:00:00.01 |       2 |
|*  2 |   index unique scan         | pk_emp |      1 |      1 |      1 |00:00:00.01 |       1 |
------------------------------------------------------------------------------------------------

predicate information (identified by operation id):
---------------------------------------------------

   2 - access("empno"=7654)

19 rows selected.

-- 通过v$sql视图获取查询sql语句的hash_value和child_number
sql> select sql_id,hash_value,child_number,sql_text from v$sql
  2  where sql_text like 'select * from scott.emp where empno%';

sql_id        hash_value child_number sql_text
------------- ---------- ------------ --------------------------------------------------
40t73tu9dst5y 2463917246            0 select * from scott.emp where empno=7654

-- 创建outline,指定为默认default分类
sql> exec dbms_outln.create_outline(2463917246,0,'default');

pl/sql procedure successfully completed.

-- session级别设置use_stored_outlines参数为true,启用outline
sql> alter session set use_stored_outlines=true;

session altered.

-- 重新执行查询,可以看到计划与原先的一致,同时在执行计划的note中显示了使用了outline "sys_outline_13080517081959001"
sql> set autotrace traceonly
sql> select * from scott.emp where empno=7654;

execution plan
----------------------------------------------------------
plan hash value: 2949544139

--------------------------------------------------------------------------------------
| id  | operation                   | name   | rows  | bytes | cost (%cpu)| time     |
--------------------------------------------------------------------------------------
|   0 | select statement            |        |     1 |    38 |     1   (0)| 00:00:01 |
|   1 |  table access by index rowid| emp    |     1 |    38 |     1   (0)| 00:00:01 |
|*  2 |   index unique scan         | pk_emp |     1 |       |     0   (0)| 00:00:01 |
--------------------------------------------------------------------------------------

predicate information (identified by operation id):
---------------------------------------------------

   2 - access("empno"=7654)

note
-----
   - outline "sys_outline_13080517081959001" used for this statement

statistics
----------------------------------------------------------
       1495  recursive calls
        147  db block gets
        262  consistent gets
          5  physical reads
        632  redo size
        896  bytes sent via sql*net to client
        512  bytes received via sql*net from client
          1  sql*net roundtrips to/from client
         24  sorts (memory)
          0  sorts (disk)
          1  rows processed

使用非default分类下的outline

复制代码 代码如下:

-- 查看当前可用的outline
sql> select category,ol_name,hintcount,sql_text from outln.ol$;

category   ol_name                         hintcount sql_text
---------- ------------------------------ ---------- --------------------------------------------------
test       my_test_outln                           6 select count(*) from scott.emp
default    sys_outline_13080517081959001           6 select * from scott.emp where empno=7654

-- 设置使用test分类下的outline
sql> alter session set use_stored_outlines=test;

session altered.

-- 执行计划note显示使用了outline "my_test_outln"
sql> set autotrace traceonly
sql> select count(*) from scott.emp;

execution plan
----------------------------------------------------------
plan hash value: 2937609675

-------------------------------------------------------------------
| id  | operation        | name   | rows  | cost (%cpu)| time     |
-------------------------------------------------------------------
|   0 | select statement |        |     1 |     1   (0)| 00:00:01 |
|   1 |  sort aggregate  |        |     1 |            |          |
|   2 |   index full scan| pk_emp |    14 |     1   (0)| 00:00:01 |
-------------------------------------------------------------------

note
-----
   - outline "my_test_outln" used for this statement

statistics
----------------------------------------------------------
         34  recursive calls
        148  db block gets
         22  consistent gets
          0  physical reads
        540  redo size
        526  bytes sent via sql*net to client
        523  bytes received via sql*net from client
          2  sql*net roundtrips to/from client
          2  sorts (memory)
          0  sorts (disk)
          1  rows processed

关于outline的一些注意事项:

1,outline匹配sql语句有点类似cusor_sharing参数中的similar,也就是说即使不同hash值的sql语句也可能使用相同的outline,如:

复制代码 代码如下:

sql> alter session set use_stored_outlines=test;

session altered.

-- 使用不相同的sql语句 同样使用了和之前相同的outline
sql> set autotrace traceonly
sql> select count(*)from scott.emp;

execution plan
----------------------------------------------------------
plan hash value: 2937609675

-------------------------------------------------------------------
| id  | operation        | name   | rows  | cost (%cpu)| time     |
-------------------------------------------------------------------
|   0 | select statement |        |     1 |     1   (0)| 00:00:01 |
|   1 |  sort aggregate  |        |     1 |            |          |
|   2 |   index full scan| pk_emp |    14 |     1   (0)| 00:00:01 |
-------------------------------------------------------------------

note
-----
   - outline "my_test_outln" used for this statement

statistics
----------------------------------------------------------
          0  recursive calls
          0  db block gets
          1  consistent gets
          0  physical reads
          0  redo size
        526  bytes sent via sql*net to client
        523  bytes received via sql*net from client
          2  sql*net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed

-- 查询v$sql可以看到两条语句是不同的
sql> select sql_id,hash_value,child_number,sql_text from v$sql
  2  where sql_text like '%scott.emp%';

sql_id        hash_value child_number sql_text
------------- ---------- ------------ --------------------------------------------------
6xydpctfbwbm6 1555967590            0 select sql_id,hash_value,child_number,sql_text fro
                                      m v$sql where sql_text like '%scott.emp%'
40t73tu9dst5y 2463917246            0 select * from scott.emp where empno=7654
abj9tmfcs15bm 2575340915            0 select count(*) from scott.emp
d16cs4nzg9vmk 1056239218            0 select count(*)from scott.emp

2,dbms_outln.create_outline中hash_value的是sql语句的hash值,不是sql_id,也不是执行计划的hash_value。

3,dbms_outln.create_outline无法像create outline语句那样自定义outline的名称,这种方式创建的outline名称是系统自动生成的,需要可以手动使用alter outline语句来修改。

4,可以通过v$sql中的outline_sid和outline_category字段查询到已经记录到大纲中的sql语句。

复制代码 代码如下:

select sql_id,hash_value,child_number,outline_sid,outline_category,sql_text from v$sql
where sql_text like '%scott.emp%'

sql_id        hash_value child_number outline_sid outline_ca sql_text
------------- ---------- ------------ ----------- ---------- ---------------------------------------------
6xydpctfbwbm6 1555967590            0                        select sql_id,hash_value,child_number,sql_tex
                                                             t from v$sql where sql_text like '%scott.emp%'
40t73tu9dst5y 2463917246            0             default    select * from scott.emp where empno=7654
abj9tmfcs15bm 2575340915            0             test       select count(*) from scott.emp
d16cs4nzg9vmk 1056239218            0             test       select count(*)from scott.emp

sql profile(sql概要文件)

sql profile基本上相当于outline的升级版本,也是功能上最强大的,很多时候都是在使用sql优化顾问(sql tuning advisor,sta)才会接触到,同outline相同,sql profile同样由一系列hint组成,使用sql profile我们可以在sql语句执行的同时后台应用这些hint从而达到维持执行计划稳定性的目的,事实上,相对outline它还具备一些特有的优势,比如允许概要文件通过忽略常量应用到多条sql语句上,同时还可以将任意hint集合与指定的sql语句结合起来!!

在使用sql profile的过程中,参数sqltune_category实现了和outline中的user_stored_outline参数一样的功能,于此同时,概要文件也会默认创建到default分类中,通过为sqltune_category参数指定不同的分类名称来启用对应分类的sql profile;通常我们都是使用sta来创建概要文件,其实这些操作都直接间接的使用了dbms_sqltune.import_sql_profile过程,调用签名如下:

复制代码 代码如下:

procedure import_sql_profile
 argument name                  type                    in/out default?
 ------------------------------ ----------------------- ------ --------
 sql_text                       clob                    in
 profile                        sqlprof_attr            in
 name                           varchar2                in     default
 description                    varchar2                in     default
 category                       varchar2                in     default
 validate                       boolean                 in     default
 replace                        boolean                 in     default
 force_match                    boolean                 in     default
procedure import_sql_profile
 argument name                  type                    in/out default?
 ------------------------------ ----------------------- ------ --------
 sql_text                       clob                    in
 profile_xml                    clob                    in
 name                           varchar2                in     default
 description                    varchar2                in     default
 category                       varchar2                in     default
 validate                       boolean                 in     default
 replace                        boolean                 in     default
 force_match                    boolean                 in     default


可以看到sql profile的创建是通过对sql_text指定hint集来完成的,并非outline中的hash_value,同时profile字段的类型显示使用的sqlprof_attr,profile_xml字段也是需要通过获取v$sql_plan视图的other_xml字段来填充hint集的,可惜的是在官档中并没有提及这一概要文件的重要过程,因此无法详细了解它的使用细节,实际使用中还是建议使用sta来完成sql profile的创建。kerry osborne曾利用该过程来实现通过sql_id来创建sql profile,同时给出了利用import_sql_profile过程自定义hint集合来强制改变执行计划的解决方案【可以访问kerryosborne.oracle-guy.com获取详细信息】

基线(baseline)

baseline更像是一个性能的指标,oracle会通过基线来维护和消除系统的性能退化,基线的核心是一套具有特定名称并与特定语句相联系的hint,它可以像概要文件一样匹配sql语句,虽然对计划的控制能力没有概要文件那么灵活,但它仍然是限制计划不稳定性的重要方法,下面是基线的一些特点:
1,基线中不存在分类category。
2,每个sql语句可以有多个基线,比如固定基线集合。
3,基线保存了hint和执行计划的hash_value,因此优化器在判定是否采用基线时还需要验证是否有对应的计划存在。
4,可以通过设置optimizer_capture_sql_plan_baselines为true来为每一条执行过的sql语句自动创建基线,默认情况下不会创建基线。
5,通过查询视图dba_sql_plan_baselines可以获得已经创建的基线。
6,使用dbms_spm.load_plans_from_cursor_cache过程可以为一条缓存的sql语句创建基线。
7,在11g中,默认会使用已经存在的基线维持执行计划的稳定性。

为指定sql语句创建基线

复制代码 代码如下:

-- 仍然使用outline中的示例查询
sql> select * from scott.emp where empno=7654;

     empno ename      job              mgr hiredate         sal       comm     deptno
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      7654 martin     salesman        7698 28-sep-81       1250       1400         30

sql> select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

plan_table_output
------------------------------------------------------------------------------------------------------------------
sql_id  40t73tu9dst5y, child number 0
-------------------------------------
select * from scott.emp where empno=7654

plan hash value: 2949544139

------------------------------------------------------------------------------------------------
| id  | operation                   | name   | starts | e-rows | a-rows |   a-time   | buffers |
------------------------------------------------------------------------------------------------
|   0 | select statement            |        |      1 |        |      1 |00:00:00.01 |       2 |
|   1 |  table access by index rowid| emp    |      1 |      1 |      1 |00:00:00.01 |       2 |
|*  2 |   index unique scan         | pk_emp |      1 |      1 |      1 |00:00:00.01 |       1 |
------------------------------------------------------------------------------------------------

predicate information (identified by operation id):
---------------------------------------------------

   2 - access("empno"=7654)

19 rows selected.

-- 创建baseline,注意参数为sql_id和plan_hash_value
sql> var ret number
sql> exec :ret := dbms_spm.load_plans_from_cursor_cache(-
>     sql_id=>'&sql_id', -
>     plan_hash_value=>&plan_hash_value,-
>     fixed=>'&fixed');
enter value for sql_id: 40t73tu9dst5y
enter value for plan_hash_value: 2949544139
enter value for fixed: no

pl/sql procedure successfully completed.

-- 再次运行查询可以发现在执行计划输出的note中显示使用了基线sql_plan_bmwra43zx42kr695cc014
sql> set autotrace traceonly
sql> select * from scott.emp where empno=7654;

execution plan
----------------------------------------------------------
plan hash value: 2949544139

--------------------------------------------------------------------------------------
| id  | operation                   | name   | rows  | bytes | cost (%cpu)| time     |
--------------------------------------------------------------------------------------
|   0 | select statement            |        |     1 |    38 |     1   (0)| 00:00:01 |
|   1 |  table access by index rowid| emp    |     1 |    38 |     1   (0)| 00:00:01 |
|*  2 |   index unique scan         | pk_emp |     1 |       |     0   (0)| 00:00:01 |
--------------------------------------------------------------------------------------

predicate information (identified by operation id):
---------------------------------------------------

   2 - access("empno"=7654)

note
-----
   - sql plan baseline "sql_plan_bmwra43zx42kr695cc014" used for this statement

statistics
----------------------------------------------------------
        747  recursive calls
         14  db block gets
        117  consistent gets
          0  physical reads
       2956  redo size
       1028  bytes sent via sql*net to client
        523  bytes received via sql*net from client
          2  sql*net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网