您好,欢迎访问一九零五行业门户网

Oracle中利用10053事件来分析Oracle是如何做出最终的执行计划

我们都知道oracle从10g开始sql语句选择什么样的执行方式,是全表扫描,还是走索引的依据是执行代价.那么我们怎么可以去看执行代价的
我们都知道oracle从10g开始sql语句选择什么样的执行方式,是全表扫描,还是走索引的依据是执行代价.那么我们怎么可以去看执行代价的信息呢?通过10053事件可以oracle依据的执行代价和如何做出执行计划的.如果我们发现某一条sql语句的执行计划和想像的不一样,我们就可以去看看oracle所使用的统计分析数据是否准确,是不是统计信息太久没有分析了,重新分析有问题的对象,最终让oracle做出正确的执行计划。
我们来做一次10053事件的示例:
sql> create table t1 as select rownum rn from dba_objects;
table created.
sql> create index ind_t1 on t1(rn);
index created.
sql> exec dbms_stats.gather_table_stats(user,'t1',cascade=>true);
pl/sql procedure successfully completed.
sql> create table t2 as select rn, 't2' name from t1 where rn table created.
sql> create index ind_t2 on t2(rn);
index created.
sql> exec dbms_stats.gather_table_stats(user,'t2',cascade=>true);
pl/sql procedure successfully completed.
sql> alter session set tracefile_identifier='mysession';
session altered.
sql> alter session set events '10053 trace name context forever,level 1';
session altered.
sql> explain plan for select t2.* from t1,t2 where t1.rn explained.
sql> alter session set events '10053 trace name context off';
session altered.
和上次讲sql_trace的时候一样,生成的trace文件的路径是$oracle_base/admin/sid/udump目录.
与sql_trace和10046事件不同的是,生成的trace文件不能用tkprof处理,只能阅读原始的trace文件.
对trace文件做一个大体的介绍:
**************************
predicate move-around (pm)
**************************
sql:******* unparsed query is *******
select t2.rn rn,t2.name name from yorker.t1 t1,yorker.t2 t2 where t1.rnfpd:   current where clause predicates in sel$1 (#0) :
         t1.rnkkogcp: try to generate transitive predicate from check constraints for sel$1 (#0)
predicates with check contraints: t1.rnafter transitive predicate generation: t1.rnfinally: t1.rnfpd:   following transitive predicates are generated in sel$1 (#0) :
         t2.rnapadrv-start: call(in-use=340, alloc=0), compile(in-use=31192, alloc=0)
kkoqbc-start
            : call(in-use=344, alloc=0), compile(in-use=31976, alloc=0)
******************************************
我们会发现oracle会对sql语句做一个转换,把它改成最符合oracle处理的语句.
比如查询条件变成了 finally: t1.rn接下来是对一些trace文件中使用的缩写的描述和绑定变量的使用:
the following abbreviations are used by optimizer trace.
cbqt - cost-based query transformation
jppd - join predicate push-down
fpd - filter push-down
pm - predicate move-around
cvm - complex view merging
spj - select-project-join
*******************************************
peeked values of the binds in sql statement
*******************************************
接下来是sql用到的表,索引的统计信息,如果我们发现这个地方和实际不符,可能需要对对象做分析了.
这些信息包括了表的行数,数据块数,行的平均长度.
列平均长度,非重复的值,空值数,密度,最小值和最大值.   
索引的高度,叶子块数目,每一个索引键值占据的块数(lb/k),每一个索引键值对应的表的数据块数目(db/k).
索引的聚合因子(cluf).
聚合因子指的是索引中键值在索引块的分布和对应的表中的数据块分布的一种关系.
当索引键值和表中数据排列顺序大致相同时,这个值就越小,当一个索引键值的数据分布在越多的表
的数据块时,这个值越大,意味着使用索引的代价越高.
***************************************
base statistical information
***********************
table stats::
  table: t2  alias: t2
    #rows: 9999  #blks:  21  avgrowlen:  6.00
  column (#1): rn(number)
    avglen: 4.00 ndv: 9999 nulls: 0 density: 1.0001e-004 min: 1 max: 9999
index stats::
  index: ind_t2  col#: 1
    lvls: 1  #lb: 21  #dk: 9999  lb/k: 1.00  db/k: 1.00  cluf: 17.00
***********************
table stats::
  table: t1  alias: t1
    #rows: 51060  #blks:  86  avgrowlen:  4.00
  column (#1): rn(number)
    avglen: 5.00 ndv: 51060 nulls: 0 density: 1.9585e-005 min: 5 max: 51055
index stats::
  index: ind_t1  col#: 1
    lvls: 1  #lb: 113  #dk: 51060  lb/k: 1.00  db/k: 1.00  cluf: 78.00
***************************************
接下来是对各种访问方式的代价的信息,通过比较所有的数据访问方式的代价,选择代价最小的方式
作为执行计划.
对于t1表,列出了
access path: tablescan 全表扫描   cost:  22.86
access path: index (index (ffs)) 索引快速扫描  cost:  27.63
access path: index (indexonly)只访问索引.(因为只用到了索引数据rn)   cost: 2.01
可以看到只访问索引的代价最小.
对于t2表有如下访问方式:(最后会走index (rangescan))
access path: tablescan cost:  6.37
access path: index (rangescan) cost: 3.01
然后再是对关联顺序的考虑:
t1关联t2
nl join(nested loops join) cost: 2051.15
sm join(sort merge join) sm cost: 6.02
ha join (hash join) ha cost: 5.52
t2关联t1
nl join(nested loops join) cost: 475.12
sm join(sort merge join) sm cost: 6.02
ha join (hash join) ha cost: 5.52
t1关联t2的cpu代价更小,最后会走t1关联t2.
single table access path
  table: t1  alias: t1    
    card: original: 51060  rounded: 95  computed: 95.02  non adjusted: 95.02
  access path: tablescan
    cost:  22.86  resp: 22.86  degree: 0
      cost_io: 21.00  cost_cpu: 10824444
      resp_io: 21.00  resp_cpu: 10824444
  access path: index (index (ffs))
    index: ind_t1
    resc_io: 26.00  resc_cpu: 9484923
    ix_sel: 0.0000e+000  ix_sel_with_filters: 1
  access path: index (ffs)
    cost:  27.63  resp: 27.63  degree: 1
      cost_io: 26.00  cost_cpu: 9484923
      resp_io: 26.00  resp_cpu: 9484923
  access path: index (indexonly)
    index: ind_t1
    resc_io: 2.00  resc_cpu: 33443
    ix_sel: 0.0018609  ix_sel_with_filters: 0.0018609
    cost: 2.01  resp: 2.01  degree: 1
  best:: accesspath: indexrange  index: ind_t1
         cost: 2.01  degree: 1  resp: 2.01  card: 95.02  bytes: 0
***************************************
single table access path
  table: t2  alias: t2    
    card: original: 9999  rounded: 99  computed: 99.01  non adjusted: 99.01
  access path: tablescan
    cost:  6.37  resp: 6.37  degree: 0
      cost_io: 6.00  cost_cpu: 2151330
      resp_io: 6.00  resp_cpu: 2151330
  access path: index (rangescan)
    index: ind_t2
    resc_io: 3.00  resc_cpu: 58364
    ix_sel: 0.009902  ix_sel_with_filters: 0.009902
    cost: 3.01  resp: 3.01  degree: 1
  best:: accesspath: indexrange  index: ind_t2
         cost: 3.01  degree: 1  resp: 3.01  card: 99.01  bytes: 0
***************************************
optimizer statistics and computations
***************************************
general plans
***************************************
considering cardinality-based initial join order.
***********************
join order[1]:  t1[t1]#0  t2[t2]#1
***************
now joining: t2[t2]#1
***************
nl join
  outer table: card: 95.02  cost: 2.01  resp: 2.01  degree: 1  bytes: 4
  inner table: t2  alias: t2
  access path: tablescan
    nl join:  cost: 475.12  resp: 475.12  degree: 0
      cost_io: 440.00  cost_cpu: 204409816
      resp_io: 440.00  resp_cpu: 204409816
kkofmx: index filter:t2.rn  access path: index (alleqjoinguess)
    index: ind_t2
    resc_io: 2.00  resc_cpu: 15463
    ix_sel: 1.0001e-004  ix_sel_with_filters: 9.9030e-007
    nl join (ordered): cost: 115.77  resp: 115.77  degree: 1
      cost_io: 115.60  cost_cpu: 950127
      resp_io: 115.60  resp_cpu: 950127
  best nl cost: 115.77
          resc: 115.77 resc_io: 115.60 resc_cpu: 950127
          resp: 115.77 resp_io: 115.60 resp_cpu: 950127
join card:  94.08 = outer (95.02) * inner (99.01) * sel (0.01)
join card - rounded: 94 computed: 94.08
sm join
  outer table:
    resc: 2.01  card 95.02  bytes: 4  deg: 1  resp: 2.01
  inner table: t2  alias: t2
    resc: 3.01  card: 99.01  bytes: 6  deg: 1  resp: 3.01
    using dmeth: 2  #groups: 1
    sort resource      sort statistics
      sort width:         231 area size:      202752 max area size:    40684544
      degree:               1
      blocks to sort:       1 row size:           17 total rows:             99
      initial runs:         1 merge passes:        0 io cost / pass:          0
      total io sort cost: 0      total cpu sort cost: 5849269
      total temp space used: 0
  sm join: resc: 6.02  resp: 6.02  [multimatchcost=0.00]
  sm cost: 6.02
     resc: 6.02 resc_io: 5.00 resc_cpu: 5941076
     resp: 6.02 resp_io: 5.00 resp_cpu: 5941076
ha join
  outer table:
    resc: 2.01  card 95.02  bytes: 4  deg: 1  resp: 2.01
  inner table: t2  alias: t2
    resc: 3.01  card: 99.01  bytes: 6  deg: 1  resp: 3.01
    using dmeth: 2  #groups: 1
    cost per ptn: 0.50  #ptns: 1
    hash_area: 0 (max=0)   hash join: resc: 5.52  resp: 5.52  [multimatchcost=0.00]
  ha cost: 5.52
     resc: 5.52 resc_io: 5.00 resc_cpu: 3025807
     resp: 5.52 resp_io: 5.00 resp_cpu: 3025807
best:: joinmethod: hash
       cost: 5.52  degree: 1  resp: 5.52  card: 94.08  bytes: 10
***********************
best so far: table#: 0  cost: 2.0057  card: 95.0186  bytes: 380
             table#: 1  cost: 5.5199  card: 94.0778  bytes: 940
***********************
join order[2]:  t2[t2]#1  t1[t1]#0
***************
now joining: t1[t1]#0
***************
nl join
  outer table: card: 99.01  cost: 3.01  resp: 3.01  degree: 1  bytes: 6
  inner table: t1  alias: t1
  access path: tablescan
    nl join:  cost: 2051.15  resp: 2051.15  degree: 0
      cost_io: 1867.00  cost_cpu: 1071678304
      resp_io: 1867.00  resp_cpu: 1071678304
  access path: index (index (ffs))
    index: ind_t1
    resc_io: 24.74  resc_cpu: 9484923
    ix_sel: 0.0000e+000  ix_sel_with_filters: 1
  inner table: t1  alias: t1
  access path: index (ffs)
    nl join:  cost: 2613.36  resp: 2613.36  degree: 0
      cost_io: 2452.00  cost_cpu: 939065714
      resp_io: 2452.00  resp_cpu: 939065714
kkofmx: index filter:t1.rn  access path: index (alleqjoinguess)
    index: ind_t1
    resc_io: 1.00  resc_cpu: 8171
    ix_sel: 1.9585e-005  ix_sel_with_filters: 3.6446e-008
    nl join (ordered): cost: 102.15  resp: 102.15  degree: 1
      cost_io: 102.00  cost_cpu: 872287
      resp_io: 102.00  resp_cpu: 872287
  best nl cost: 102.15
          resc: 102.15 resc_io: 102.00 resc_cpu: 872287
          resp: 102.15 resp_io: 102.00 resp_cpu: 872287
join card:  94.08 = outer (99.01) * inner (95.02) * sel (0.01)
join card - rounded: 94 computed: 94.08
sm join
  outer table:
    resc: 3.01  card 99.01  bytes: 6  deg: 1  resp: 3.01
  inner table: t1  alias: t1
    resc: 2.01  card: 95.02  bytes: 4  deg: 1  resp: 2.01
    using dmeth: 2  #groups: 1
    sort resource      sort statistics
      sort width:         231 area size:      202752 max area size:    40684544
      degree:               1
      blocks to sort:       1 row size:           15 total rows:             95
      initial runs:         1 merge passes:        0 io cost / pass:          0
      total io sort cost: 0      total cpu sort cost: 5847820
      total temp space used: 0
  sm join: resc: 6.02  resp: 6.02  [multimatchcost=0.00]
  sm cost: 6.02
     resc: 6.02 resc_io: 5.00 resc_cpu: 5939627
     resp: 6.02 resp_io: 5.00 resp_cpu: 5939627
ha join
  outer table:
    resc: 3.01  card 99.01  bytes: 6  deg: 1  resp: 3.01
  inner table: t1  alias: t1
    resc: 2.01  card: 95.02  bytes: 4  deg: 1  resp: 2.01
    using dmeth: 2  #groups: 1
    cost per ptn: 0.50  #ptns: 1
    hash_area: 0 (max=0)   hash join: resc: 5.52  resp: 5.52  [multimatchcost=0.00]
  ha cost: 5.52
     resc: 5.52 resc_io: 5.00 resc_cpu: 3026007
     resp: 5.52 resp_io: 5.00 resp_cpu: 3026007
join order aborted: cost > best plan cost
***********************
(newjo-stop-1) k:0, spcnt:0, perm:2, maxperm:2000
*********************************
number of join permutations tried: 2
*********************************
(newjo-save)    [1 0 ]
final - all rows plan:  best join order: 1
  cost: 5.5199  degree: 1  card: 94.0000  bytes: 940
  resc: 5.5199  resc_io: 5.0000  resc_cpu: 3025807
  resp: 5.5199  resp_io: 5.0000  resc_cpu: 3025807
kkoipt: query block sel$1 (#0)
******* unparsed query is *******
select t2.rn rn,t2.name name from yorker.t1 t1,yorker.t2 t2 where t1.rnkkoqbc-end
          : call(in-use=31732, alloc=0), compile(in-use=33436, alloc=0)
apadrv-end: call(in-use=31732, alloc=0), compile(in-use=34024, alloc=0)
sql_id=ar0vn3xs804bs.
current sql statement for this session:
explain plan for select t2.* from t1,t2 where t1.rn  
============
plan table
============
------------------------------------------------+-----------------------------------+
| id  | operation                     | name    | rows  | bytes | cost  | time      |
------------------------------------------------+-----------------------------------+
| 0   | select statement              |         |       |       |     6 |           |
| 1   |  hash join                    |         |    94 |   940 |     6 |  00:00:01 |
| 2   |   index range scan            | ind_t1  |    95 |   380 |     2 |  00:00:01 |
| 3   |   table access by index rowid | t2      |    99 |   594 |     3 |  00:00:01 |
| 4   |    index range scan           | ind_t2  |    99 |       |     2 |  00:00:01 |
------------------------------------------------+-----------------------------------+

其它类似信息

推荐信息