【Oracle SQL优化】Oracle优化器如何计算索引成本

我们介绍过  【CBO Optimizer优化器】IX_SEL索引选择率  在这个基础上介绍Oracle CBO优化器如何计算索引成本Index Cost

 

 

基本上这些信息可以通过10053 trace去猜测和获得,但下面仅仅是简单的组合例子解释CBO如何计算index cost的公式,实际的情况千差万别,复杂得多。

 

基本上索引的成本被分成2部分:

  •  访问索引块的成本
  • 对应去表上查找的成本

 

比较细的粒度的公式如下:
Selectivity of the combined leading columns * Number of Blocks in the Index
+
Selectivity of all indexed columns * Number of blocks likely to be visited in the
table (ie Clustering factor)
+ usually more minor points such as levels in the index +CPU

 

但大多数情况下 我们用下面的公式就可以了:

 

 

Index cost + Table look up costs:
(ix_sel: * #LB: ) + (ix_sel_with_filters: * CLUF: )

 

IX_SEL常为查询中参考到的所有被索引的字段的DISTINCT值累乘,如在查询中涉及到3个索引字段 A,B,C则选择性为:

1/ NDV( A * B * C)

NDV 为 number of distinct values

 

 

举个例子来说

 

 

select *
from zzrfaccna
where rclnt=:A0 and rldnr=:A1 and rbukrs=:A2
and ryear=:A3 and racct=:A4 and poper=:A5

在10053中可以看到

Column (#14): RBUKRS(VARCHAR2)
AvgLen: 5.00 NDV: 20 Nulls: 0 Density: 0.05
Column (#15): RACCT(VARCHAR2)
AvgLen: 11.00 NDV: 2213 Nulls: 0 Density: 4.5188e-04
Index: ZZRFACCNA~Z02 Col#: 14 15 16 21 22
LVLS: 4 #LB: 4180960 #DK: 222921 LB/K: 18.00 DB/K: 918.00 CLUF: 204796900.00

Access Path: index (RangeScan)
Index: ZZRFACCNA~Z02
resc_io: 4727.00 resc_cpu: 48015032
ix_sel: 2.2594e-05 ix_sel_with_filters: 2.2594e-05
Cost: 4753.68 Resp: 4753.68 Degree: 1

 

这里仅仅索引的前导列用来计算IX_SEL,所以这里的IX_SEL为

 

1/ (20 * 2213) = 1 / 44260 = 2.2593 e-5

 

由于查询中没有更多索引字段,所以其ix_sel_with_filters 等于IX_SEL

 

则该成本Cost计算为

 

( ix_sel: 2.2594e-05 * #LB: 4180960 ) + ( ix_sel_with_filters: 2.2594e-05 *
CLUF: 204796900.00 )
(94) + (4627) (+ LVL + CPU) = ~ 4727

 

如果自己搞不定可以找诗檀软件专业ORACLE数据库服务团队成员帮您恢复!

诗檀软件专业数据库修复团队

服务热线 : 13764045638   QQ号:47079569    邮箱:[email protected]


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *