ITPub博客

首页 > 应用开发 > IT综合 > histogram一点研究(待整理)

histogram一点研究(待整理)

原创 IT综合 作者:foreverlee 时间:2006-03-22 23:57:00 0 删除 编辑

一 基于值的histogram

1
corp表sp_id列的distinct key为22
SQL> select count(distinct sp_id) from corp;

COUNT(DISTINCTSP_ID)
--------------------
22

[@more@]

一 基于值的histogram

1
corp表sp_id列的distinct key为22
SQL> select count(distinct sp_id) from corp;

COUNT(DISTINCTSP_ID)
--------------------
22

2 使用以下语句产生柱状图数据.默认会产生75个buckets来表示数据分布

SQL> analyze table corp compute statistics for columns sp_id;

Table analyzed.

3 当histogram buckets>=表的distinct key时,那么Oracle会使用基于值的histogram,每个值将会占据一个bucket

SQL> select ENDPOINT_NUMBER,ENDPOINT_VALUE,ENDPOINT_ACTUAL_VALUE from user_histograms
2 where TABLE_NAME='CORP'
3 and COLUMN_NAME='SP_ID';


基于值的Histogram,
ENDPOINT_VALUE表示表CORP SP_ID列的值
ENDPOINT_NUMBER表示该值的分布

ENDPOINT_NUMBER ENDPOINT_VALUE ENDPOINT_ACTUAL_VALUE
--------------- -------------- ------------------------------
242 6
369 9
384 11
413 13
431 14
447 15
489 16
500 17
513 18
514 19
518 20

ENDPOINT_NUMBER ENDPOINT_VALUE ENDPOINT_ACTUAL_VALUE
--------------- -------------- ------------------------------
523 21
532 22
535 23
537 24
546 25
549 26
556 27
559 28
563 29
566 30
586 31

22 rows selected.

这里我们也可以通过以下查询得到sp_id=6时的纪录条数
SQL> select count(*) from corp where sp_id=6;

COUNT(*)
----------
242

369-242=127
SQL> select count(*) from corp where sp_id=9;

COUNT(*)
----------
127


二 基于高度的histogram

1
for columns sp_id size 10表示十个Buckets表示数据分布

SQL> analyze table corp compute statistics for columns sp_id size 10;

Table analyzed.

SQL> SELECT column_name, num_distinct, num_buckets
2 FROM USER_TAB_COL_STATISTICS
3 WHERE table_name = 'CORP' and column_name ='SP_ID';

COLUMN_NAME NUM_DISTINCT NUM_BUCKETS
------------------------------ ------------ -----------
SP_ID 22 5


当histogram buckets的数量(10)少于列的distinct(22) value时,oracle会采用基于高度的直方图反映数据分布,每个bucket容纳相同数量的值。


表corp共586条数据,第0-8个Buckets每个Bucket保存58条纪录,第9个保存64条纪录
SQL> select count(*) from corp;

COUNT(*)
----------
586

SQL> select 586/10 from dual;

586/10
----------
58.6

前4个(1,2,3,4)buckets保存sp_id=6的纪录
4*58=232
所以sp_id=6的纪录大约在232左右
SQL> select ENDPOINT_NUMBER,ENDPOINT_VALUE,ENDPOINT_ACTUAL_VALUE from user_histograms
2 where TABLE_NAME='CORP'
3 and COLUMN_NAME='SP_ID';

ENDPOINT_NUMBER ENDPOINT_VALUE ENDPOINT_ACTUAL_VALUE
--------------- -------------- ------------------------------
4 6
6 9
7 13
8 16
9 22
10 31

6 rows selected.

实际查询为242
SQL> select count(*) from corp where sp_id=6;

COUNT(*)
----------
242

6-4=2个(4,5)Buckets保存sp_id=9的纪录
2*58=116 实际127条纪录
SQL> select count(*) from corp where sp_id=9;

COUNT(*)
----------
127

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/76065/viewspace-822222/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-11-26

  • 博文量
    72
  • 访问量
    1370824