CBO学习----03--选择率(Selectivity)(二)

2014-11-24 15:58:20 · 作者: · 浏览: 5
**********************************/

本章代码附件中:
in_list_02.sql
将月份从12扩大到1000,density为0.001
获得列表3-1,用来对比8i到10g的计算值,版本间影响不大
/**************************************************************************************************************************************/
本章代码附件中:
oddities.sql
在存有重复值、空值、过界值时,计算rows的状况,10204明显有所改善,考虑了最大最小值(书中原版本未考虑过界值的问题)


条件 8i 92/10102 10204
month_no = 25  100 100 1
month_no in (4, 4) 100 100 100
month_no in (3, 25)  192 200 100
month_no in (3, 25, 26)  276 300 101
month_no in (3, 25, 25, 26)  276 300 101
month_no in (3, 25, null)  276 300 200
month_no in (:b1, :b2, :b3)  276 300 300
仅仅在有空值时,不能正确计算rows,但也比之前版本误差小。


/**************************************************************************************************************************************/

10104的线性衰变
oracle在10.2之前采用过一个更为复杂的线性衰变算法,即超过最大值或低于最小值时,单值rows的斜率为 "1/最大值-最小值"。
通过上面10204的测试,这个算法在新版本中被取代。
本章代码附件中:
in_list_10g.sql
在10104中的结果:
条件 10102 10104
month_no = 13 100 91
month_no = 15 100 73
month_no in (13,15) 200 164
month_no in (16,18) 200 109
10104比10102略有改进,10204比10104改进就更加明显了,虽然还有缺陷(null上),但说明oracle的opt团队在不断的更新代码。
/**************************************************************************************************************************************/
3.4 Range
本章代码附件中:
ranges.sql
总结各个版本,统计如下:
号 谓词 8i 92/10102 10204 数学表达式 真实值
1 month_no > 8  437 436 436 (8, 400
2 month_no >= 8  537 536 536 [8, 500
3 month_no < 8  764 764 764  ,8) 700
4 month_no <= 8  864 864 864  ,8] 800
5 month_no between 6 and 9  528 527 527 [6,9] 400
6 month_no >= 6 and month_no <= 9  528 527 527 [6,9] 400
7 month_no >= 6 and month_no < 9  428 427 427 [6,9) 300
8 month_no > 6 and month_no <= 9  428 427 427 (6,9] 300
9 month_no > 6 and month_no < 9  328 327 327 (6,9) 200
10 month_no > :b1  60 60 101 (:b1,            
11 month_no >= :b1  60 60 101 [:b1,            
12 month_no < :b1  60 60 101  ,:b1)           
13 month_no <= :b1  60 60 101  ,:b1]           
14 month_no between :b1 and :b2  4 3 9 [:b1,:b2]        
15 month_no >= :b1 and month_no <= :b2  4 3 9 [:b1,:b2]        
16 month_no >= :b1 and month_no < :b2  4 3 9 [:b1,:b2)        
17 month_no > :b1 and month_no < :b2  4 3 9 (:b1,:b2)        
18 month_no > :b1 and month_no <= :b2  4 3 9 (:b1,:b2]        
19 month_no > 12  100 100 100 (12, 0
20 month_no between 25 and 30  100 100 100 [25,30] 0

其中8i的4和9i的3,是没有区别的(10进制和二进制转换时有个小数),只是8i在计算式使用上浮,9i用的舍入
从上表发现的几条规律:
(1)"(" 和 "]"的差距为100
(2)超过边界值为100
(3)有绑定变量的rows,开闭区间并无影响
(4)10-13行,单个绑定变量的range,前期版本定为5%(1200*5%=60),10204之后较为复杂(有待研究)
(5)14-18行,5%*5%=2.5/1000,1200*5%*5%=3行
(6)19-20行,超出最大最小值范围时,给出一个固定的选择率。本章代码selectivity_one.sql给出了该环节9i的测试代码。
本章代码附件中:
selectivity_one.sql
构建一个4列的nologging表,3000行;对比4个边界上的定值查询和">"查询,rows是一样的。
说明超过边界值的查询所给出的选择率,跟等于边界值查询的选择率是一样的。
/**************************************************************************************************************************************/
CARDINALITY
基数的变化,对于执行计划的后续选择至关重要


/**************************************************************************************************************************************/
绑定变量和区间
因为colx like 'A%'可以等效为colx >='A' and colx <='B';colx like :b1似乎也可以等效为between and的结构;但是他们两个的选择率是不同的,前者是5%后者是5%*5%。

本章代码附件中:
like_test.sql
构建一个10W行的表,绑定变量查询的rows为5000,正好是5%;其他定值查询都不同;但使用lower(col) like的方式,选择率也变为5%。
说明opt处理谓词表达式时,是按照变量来处理的,且为单个变量。

/**************************************************************************************************************************************/
Selectivity = “required range” / “total available range”

下面通过几个Case,来验证上面公式,并得出前面列表的结果。
Case 1
month_no > 8
(8,)
Selectivity = (high_value – limit) / (high_value – low_value) = (12 – 8) / (12 – 1) = 4/11
Cardinality = 1,200 * 4 / 11 = 436.363636

Case 2
month_no >= 8
[8,)
Selectivity = (high_value – limit) / (high_value – low_value) + 1/num_distinct = 4/11 + 1/12
Cardinality = 1,200 * (4/11 + 1/12) = 536.363636

Cases 3 and 4
Selectivity (3) = (limit – low_value) / (high_value – low_value) = (8 – 1) / (12 – 1) = 7/11
Selectivity (4) = (8 – 1) / (12 – 1) + 1/12 = 7/11 + 1/12

Cases 5 and 6
month_no between 6 and 9
Selectivity = (9 – 6) / (12 – 1) + 1/12 + 1/12             --( >= , <= )

Cases 7, 8, 9
Selectivity (7) = (9 – 6) / (12 – 1) + 1/12                   --( >= , < )
Selectivity (8) = (9 – 6) / (12 – 1) + 1/12                   --( > , <= )
Selectivity (9) = (9 – 6) / (12 – 1)                               --( > , <)

/**************************************************************************************************************************************/
BIND VARIABLE PEEKING(绑定变量窥视)

绑定变量可以共享游标,使OLTP减少了大量硬解析;但本