公式摘抄于《催华-基于ORACLE的SQL优化》
转载请注明链接地址:http://blog.csdn.net/launch_225/article/details/25432661
SQL> select * from t1; N1 V1 ---------- -------- 1 a 2 b 2 c 4 c 5 d 6 e 6 f 6 f 9 g 10 h 11 i N1 V1 ---------- -------- 12 i 12 i 14 j 15 k 16 l 16 m 18 n 18 rows selected.
1. ====>没有直方图的预估结果集行数 SQL> select count(1) from t1; COUNT(1) ---------- 18 SQL> select count(distinct n1) from t1; COUNT(DISTINCTN1) ----------------- 13 SQL> select min(n1),max(n1) from t1; MIN(N1) MAX(N1) ---------- ---------- 1 18 TABLE_NAME COLUMN_NAME DENSITY NUM_BUCKETS HISTOGRAM 1 T1 N1 0.0769230769230769 1 NONE 2 T1 V1 0.0714285714285714 1 NONE 1.1=>范围查询 ------------------------------------------------------------------------------ (1)目标列大于指定的val,且位于low_value and high_value之间 selectivity=((HIGH_VALUE-VAL)/(HIGH_VALUE-LOW_VALUE))* NULL_ADJUST NULL_ADJUST=(NUM_ROWS-NUM_NULLS)/NUM_ROWS SQL> select ((18-5)/(18-1))*1*18 from dual; ((18-5)/(18-1))*1*18 -------------------- 13.7647059 1* select * from t1 where n1>5 SQL> / Execution Plan ---------------------------------------------------------- Plan hash value: 1577308413 ----------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ----------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 14 | 70 | 2 (0)| 00:00:01 | | 1 | TABLE ACCESS BY INDEX ROWID| T1 | 14 | 70 | 2 (0)| 00:00:01 | |* 2 | INDEX RANGE SCAN | IDX_T1_N1 | 14 | | 1 (0)| 00:00:01 | ----------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("N1">5) (2)目标列小于指定的val,且位于low_value and high_value之间 selectivity=((VAL-LOW_VALUE)/(HIGH_VALUE-LOW_VALUE))* NULL_ADJUST NULL_ADJUST=(NUM_ROWS-NUM_NULLS)/NUM_ROWS SQL> select ((5-1)/(18-1))*1*18 from dual; ((5-1)/(18-1))*1*18 ------------------- 4.23529412 SQL> select * from t1 where n1<5 2 / Execution Plan ---------------------------------------------------------- Plan hash value: 1577308413 ----------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ----------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 4 | 20 | 2 (0)| 00:00:01 | | 1 | TABLE ACCESS BY INDEX ROWID| T1 | 4 | 20 | 2 (0)| 00:00:01 | |* 2 | INDEX RANGE SCAN | IDX_T1_N1 | 4 | | 1 (0)| 00:00:01 | ----------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("N1"<5) (3)目标列>=指定的val,且位于low_value and high_value之间 selectivity=((HIGH_VALUE-VAL)/(HIGH_VALUE-LOW_VALUE)+1/NUM_DISTINCT)* NULL_ADJUST NULL_ADJUST=(NUM_ROWS-NUM_NULLS)/NUM_ROWS SQL> select ((18-5)/(18-1)+1/13)*1*18 from dual; ((18-5)/(18-1)+1/13)*1*18 ------------------------- 15.1493213 SQL> select * from t1 where n1>=5; Execution Plan ---------------------------------------------------------- Plan hash value: 1577308413 ----------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ----------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 15 | 75 | 2 (0)| 00:00:01 | | 1 | TABLE ACCESS BY INDEX ROWID| T1 | 15 | 75 | 2 (0)| 00:00:01 | |* 2 | INDEX RANGE SCAN | IDX_T1_N1 | 15 | | 1 (0)| 00:00:01 | ----------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("N1">=5) (4)目标列<=指定的val,且位于low_value and high_value之间 selectivity=((VAL-LOW_VALUE)/(HIGH_VALUE-LOW_VALUE)+1/NUM_DISTINCT)* NULL_ADJUST NULL_ADJUST=(NUM_ROWS-NUM_NULLS)/NUM_ROWS SQL> select ((5-1)/(18-1)+1/13)*1*18 from dual; ((5-1)/(18-1)+1/13)*1*18 ------------------------ 5.6199095 SQL> select * from t1 where n1<=5; Execution Plan ---------------------------------------------------------- Plan hash value: 1577308413 ----------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ----------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 6 | 30 | 2 (0)| 00:00:01 | | 1 | TABLE ACCESS BY INDEX ROWID| T1 | 6 | 30 | 2 (0)| 00:00:01 | |* 2 | INDEX RANGE SCAN | IDX_T1_N1 | 6 | | 1 (0)| 00:00:01 | ----------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("N1"<=5) (5)列位于指定值VAL1和VAL2之间,且VAL1和VAL2在LOW_VALUE和HIGH_VALUE范围 selectivity=((val2-val1)/(HIGH_VALUE-LOW_VALUE)+2/NUM_DISTINCT)* NULL_ADJUST NULL_ADJUST=(NUM_ROWS-NUM_NULLS)/NUM_ROWS SQL> select ((10-5)/(18-1)+2/13)*1*18 from dual; ((10-5)/(18-1)+2/13)*1*18 ------------------------- 8.06334842 SQL> select * from t1 where n1 between 5 and 10; Execution Plan ---------------------------------------------------------- Plan hash value: 1577308413 ----------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ----------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 8 | 40 | 2 (0)| 00:00:01 | | 1 | TABLE ACCESS BY INDEX ROWID| T1 | 8 | 40 | 2 (0)| 00:00:01 | |* 2 | INDEX RANGE SCAN | IDX_T1_N1 | 8 | | 1 (0)| 00:00:01 | ----------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("N1">=5 AND "N1"<=10) ------------------------------------------------------------------------------ 1.2 =》等值查询 (1) 等值查询且列上没有NULL值没有直方图的计算公式 selectivity_without_null=(1/num_distinct) SQL> select 1/13*18 from dual; 1/13*18 ---------- 1.38461538 SQL> select * from t1 where n1=8; Execution Plan ---------------------------------------------------------- Plan hash value: 1577308413 ----------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ----------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 5 | 2 (0)| 00:00:01 | | 1 | TABLE ACCESS BY INDEX ROWID| T1 | 1 | 5 | 2 (0)| 00:00:01 | |* 2 | INDEX RANGE SCAN | IDX_T1_N1 | 1 | | 1 (0)| 00:00:01 | ----------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("N1"=8) (2)目标列没有直方图且有NULL值(通用于没有直方图时列的选择率的计算) selectivity_with_null=(1/num_distinct) * ((num_rows-num_nulls)/num_rows) SQL> select 1/13*((18-0)/18)*18 from dual; 1/13*((18-0)/18)*18 ------------------- 1.38461538 --不存在空值也通用 SQL> select * from t1 where n1=8; Execution Plan ---------------------------------------------------------- Plan hash value: 1577308413 ----------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ----------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 5 | 2 (0)| 00:00:01 | | 1 | TABLE ACCESS BY INDEX ROWID| T1 | 1 | 5 | 2 (0)| 00:00:01 | |* 2 | INDEX RANGE SCAN | IDX_T1_N1 | 1 | | 1 (0)| 00:00:01 | ----------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("N1"=8)