Oracle 执行计划(5)—cost成本之索引范围扫描-B树索引
转自 http://blog.csdn.net/zengmuansha/article/details/7490234
SQL> select * from t1 where a<600 ;
已选择599行。
已用时间: 00: 00: 00.03
执行计划
----------------------------------------------------------
Plan hash value: 2474755989
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 593 | 1779 | 3 (0)| 00:00:01 |
|* 1 | INDEX RANGE SCAN| T1_IND | 593 | 1779 | 3 (0)| 00:00:01 |
---------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("A"<600)
计算所用公式
INDEX
cost = blevel +ceiling(leaf_blocks * effective index selectivity) +ceiling(clustering_factor * effective table selectivity)
SQL> execute dbms_stats.gather_table_stats('ZENGMUANSHA','T1');
SQL> select column_name,NUM_NULLS,NUM_DISTINCT,density from user_tab_col_statistics where table_name='T1';
COLUMN_NAME NUM_NULLS NUM_DISTINCT DENSITY LOW_VALUE HIGH_VALUE
------------------------------ ---------- ------------ ----------------------
A 0 9926 0.00010074 C104 C302
SQL> select LEAF_BLOCKS,BLEVEL ,clustering_factor from user_indexes where index_name='T1_IND';
LEAF_BLOCKS BLEVEL CLUSTERING_FACTOR
----------- ---------- -----------------
21 1 16
先套下公式看看
COST=1+CEIL(21*EFFECTIVE INDEX SELECTIVITY)+CEIL(16*EFFECTIVE TABLE SELECTIVITY)
有效表选择率和有效索引选择率 是指能定位索引和表的谓词.
这里只有一个谓词 a<600 并且它是表和索引的定位的谓词.
选择率 这里就要回顾 Oracle 执行计划(2)-基数 cardinality
http://blog.csdn.net/zengmuansha/article/details/7484523
关于谓词区间的选择率计算
公式=需要空间除以可用空间
可用空间=high_vlaue-low_value=12-1=11
需要空间=(high_vlaue-limit) 或(limit-low_value)或(high_vlaue-low_value)
1 month_no>8 (high_vlaue-limit)/(high_vlaue-low_value)=(12-8)/11=4/11
A<600 =>(limit- low_value)/(high_vlaue-low_value)
=>(limit- low_value)/(high_vlaue-low_value)
=>(600-C104)/(C302-C104)
不知道如何算还是采用下面的吧!
SQL> SELECT MAX(A),MIN(A) FROM T1;
MAX(A) MIN(A)
---------- ----------
10000 1
=>(600-1)/(10000-1) =0.059905990599059905990599059905991
COST=1+CEIL(21*EFFECTIVE INDEX SELECTIVITY)+CEIL(16*EFFECTIVE TABLE SELECTIVITY)
=1+CEIL(21*0.05990)+CEIL(16*0.05990)
=1+ceil(1.2579)+ceil(0.9584)
=1+1+1
=3