- 最后登录
- 2023-8-16
- 在线时间
- 1686 小时
- 威望
- 2135
- 金钱
- 50532
- 注册时间
- 2011-10-12
- 阅读权限
- 200
- 帖子
- 5207
- 精华
- 39
- 积分
- 2135
- UID
- 2
|
4#
发表于 2013-3-14 20:41:24
some test- Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - 64bit Production
- With the Partitioning, OLAP, Data Mining and Real Application Testing options
- SQL>
- SQL> conn maclean/maclean
- Connected.
- DROP TABLE tab1;
- DROP TABLE tab2;
- create table tab_simple as select rownum t1 from dual connect by level<=10000;
- exec dbms_stats.gather_table_stats(user,'TAB_SIMPLE');
-
- alter session set events '10053 trace name context forever,level 1';
- SQL> select /*+ maclean */ 1 from tab_simple where t1=10;
- 1
- ----------
- 1
- alter session set events '10053 trace name context off';
-
- ***************************************
- SINGLE TABLE ACCESS PATH
- -----------------------------------------
- BEGIN Single Table Cardinality Estimation
- -----------------------------------------
- Column (#1): T1(NUMBER)
- AvgLen: 4.00 NDV: 10000 Nulls: 0 Density: 1.0000e-04 Min: 1 Max: 10000
- Table: TAB_SIMPLE Alias: TAB_SIMPLE
- Card: Original: 10000 Rounded: 1 Computed: 1.00 Non Adjusted: 1.00
-
-
- 没有histogram 的情况下 :
- 计算基数= 原始基数 * SEL 选择率
- A4Null = (原始基数 - Nnulls)/ 原始基数
- 等式谓词情况下:
- 选择性= (1/NDV) * A4Null
- 在上述例子中 是 10000 * (1/10000) = 1
- exec dbms_stats.gather_table_stats(user,'TAB_SIMPLE',method_opt=>'FOR ALL COLUMNS SIZE 254');
- alter session set events '10053 trace name context forever,level 1';
- select /*+ maclean1 */ 2 from tab_simple where t1=10;
- alter session set events '10053 trace name context off';
-
-
-
-
- SINGLE TABLE ACCESS PATH
- -----------------------------------------
- BEGIN Single Table Cardinality Estimation
- -----------------------------------------
- Column (#1): T1(NUMBER)
- AvgLen: 4.00 NDV: 10000 Nulls: 0 Density: 1.0000e-04 Min: 1 Max: 10000
- Histogram: HtBal #Bkts: 254 UncompBkts: 254 EndPtVals: 255
- Table: TAB_SIMPLE Alias: TAB_SIMPLE
- Card: Original: 10000 Rounded: 1 Computed: 1.00 Non Adjusted: 1.00
-
-
-
-
-
- 单表 + 等式谓词 + 高度平衡直方图
-
- 计算基数= 原始基数 * SEL 选择率
-
-
- NewDensity /* 10.2.0.4 and 11g */
- = (Buckets_total - Buckets_all_popular_values) / Buckets_total
- / (NDV - popular_values.COUNT)
- 对于非popular 值 其SEL 选择率= Density * A4Nulls =1.0000e-04
-
-
-
-
-
-
- SQL> insert into tab_simple select 99999999 from dual connect by level <=30000;
- 30000 rows created.
- SQL> commit;
- Commit complete.
-
- exec dbms_stats.gather_table_stats(user,'TAB_SIMPLE',method_opt=>'FOR ALL COLUMNS SIZE 254');
- alter session set events '10053 trace name context forever,level 1';
- select /*+ maclean2 */ 3 from tab_simple where t1=99999999;
- alter session set events '10053 trace name context off';
-
- DBA_TAB_HISTOGRAMS
- 、
- SINGLE TABLE ACCESS PATH
- -----------------------------------------
- BEGIN Single Table Cardinality Estimation
- -----------------------------------------
- Column (#1): T1(NUMBER)
- AvgLen: 6.00 NDV: 9857 Nulls: 0 Density: 2.5166e-05 Min: 5 Max: 99999999
- Histogram: HtBal #Bkts: 254 UncompBkts: 254 EndPtVals: 65
- Table: TAB_SIMPLE Alias: TAB_SIMPLE
- Card: Original: 39738 Rounded: 29804 Computed: 29803.50 Non Adjusted: 29803.50
-
-
-
- sel = round(1- 65/254) * 39738= 0.75 * 39738= 29803.50
-
-
- insert into tab_simple select 7777777777 from dual connect by level <=60000;
- commit;
-
- exec dbms_stats.gather_table_stats(user,'TAB_SIMPLE',method_opt=>'FOR ALL COLUMNS SIZE 254');
-
- alter session set events '10053 trace name context forever,level 1';
- select /*+ maclean3 */ 4 from tab_simple where t1=7777777777;
- alter session set events '10053 trace name context off';
-
- SQL> select column_name,ENDPOINT_NUMBER,ENDPOINT_VALUE from dba_tab_histograms where table_name='TAB_SIMPLE';
- COLUM ENDPOINT_NUMBER ENDPOINT_VALUE
- ----- --------------- --------------
- T1 0 1
- T1 1 394
- T1 2 788
- T1 3 1182
- T1 4 1576
- T1 5 1970
- T1 6 2364
- T1 7 2758
- T1 8 3152
- T1 9 3546
- T1 10 3940
- COLUM ENDPOINT_NUMBER ENDPOINT_VALUE
- ----- --------------- --------------
- T1 11 4334
- T1 12 4728
- T1 13 5122
- T1 14 5516
- T1 15 5910
- T1 16 6304
- T1 17 6698
- T1 18 7092
- T1 19 7486
- T1 20 7880
- T1 21 8274
- COLUM ENDPOINT_NUMBER ENDPOINT_VALUE
- ----- --------------- --------------
- T1 22 8668
- T1 23 9062
- T1 24 9456
- T1 25 9850
- T1 101 99999999
- T1 254 7777777777
-
- Table Stats::
- Table: TAB_SIMPLE Alias: TAB_SIMPLE
- #Rows: 100000 #Blks: 374 AvgRowLen: 6.00
- ***************************************
- SINGLE TABLE ACCESS PATH
- -----------------------------------------
- BEGIN Single Table Cardinality Estimation
- -----------------------------------------
- Column (#1): T1(NUMBER)
- AvgLen: 7.00 NDV: 10002 Nulls: 0 Density: 9.8425e-06 Min: 1 Max: 7777777777
- Histogram: HtBal #Bkts: 254 UncompBkts: 254 EndPtVals: 28
- Table: TAB_SIMPLE Alias: TAB_SIMPLE
- Card: Original: 100000 Rounded: 60039 Computed: 60039.37 Non Adjusted: 60039.37
-
- sel = round(1- 101/254) * 100000 = 60236.22047244094
-
-
- col column_name for a5
-
- select column_name,ENDPOINT_NUMBER,ENDPOINT_VALUE from dba_tab_histograms where table_name='TAB_SIMPLE';
-
-
-
-
-
-
- insert into tab_simple select 540404 from dual connect by level <=20000;
- commit;
-
- exec dbms_stats.gather_table_stats(user,'TAB_SIMPLE',method_opt=>'FOR ALL COLUMNS SIZE 75');
-
- alter session set events '10053 trace name context forever,level 1';
- select /*+ maclean3 */ 5 from tab_simple where t1=540404;
- alter session set events '10053 trace name context off';
-
-
- SQL> select column_name,ENDPOINT_NUMBER,ENDPOINT_VALUE from dba_tab_histograms where table_name='TAB_SIMPLE';
- COLUM ENDPOINT_NUMBER ENDPOINT_VALUE
- ----- --------------- --------------
- T1 0 4
- T1 1 1593
- T1 2 3286
- T1 3 4817
- T1 4 6437
- T1 5 8022
- T1 6 9639
- T1 18 540404
- T1 37 99999999
- T1 75 7777777777
- Table Stats::
- Table: TAB_SIMPLE Alias: TAB_SIMPLE
- #Rows: 120592 #Blks: 374 AvgRowLen: 6.00
- ***************************************
- SINGLE TABLE ACCESS PATH
- -----------------------------------------
- BEGIN Single Table Cardinality Estimation
- -----------------------------------------
- Column (#1): T1(NUMBER)
- AvgLen: 7.00 NDV: 9650 Nulls: 0 Density: 8.2927e-06 Min: 4 Max: 7777777777
- Histogram: HtBal #Bkts: 75 UncompBkts: 75 EndPtVals: 10
- Table: TAB_SIMPLE Alias: TAB_SIMPLE
- Card: Original: 120592 Rounded: 19295 Computed: 19294.72 Non Adjusted: 19294.72
-
-
- ((18-6)/75) * 120592= 19294.72
-
- select /*+ maclean3 */ 5 from tab_simple where t1=5;
- SQL> select /*+ maclean3 */ 5 from tab_simple where t1=5;
- 5
- ----------
- 5
- Execution Plan
- ----------------------------------------------------------
- Plan hash value: 4087309527
- --------------------------------------------------------------------------------
- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
- --------------------------------------------------------------------------------
- | 0 | SELECT STATEMENT | | 1 | 6 | 85 (2)| 00:00:02 |
- |* 1 | TABLE ACCESS FULL| TAB_SIMPLE | 1 | 6 | 85 (2)| 00:00:02 |
- --------------------------------------------------------------------------------
- Predicate Information (identified by operation id):
- ---------------------------------------------------
- 1 - filter("T1"=5)
- Statistics
- ----------------------------------------------------------
- 1 recursive calls
- 0 db block gets
- 365 consistent gets
- 0 physical reads
- 0 redo size
- 508 bytes sent via SQL*Net to client
- 492 bytes received via SQL*Net from client
- 2 SQL*Net roundtrips to/from client
- 0 sorts (memory)
- 0 sorts (disk)
- 1 rows processed
- 0.0000082927*120592= 1
-
-
-
-
-
-
-
-
-
-
复制代码 |
|