[PT]Column Histogram Statistics

(The descirption is from Oracle Document: Performance Tuning)

Column statistics may be stored as histograms. These histograms provide accurate estimates of the distribution of column data. Histograms provide improved selectivity estimates in the presence of data skew, resulting in optimal execution plans with nonuniform data distributions.
Oracle uses two types of histograms for column statistics: height-balanced histograms and frequency histograms. The type of histogram is stored in the HISTOGRAM column of the *TAB_COL_STATISTICS views (USER and DBA). This column can have values of HEIGHT BALANCED, FREQUENCY, or NONE.

Height-Balanced HistogramsIn a height-balanced histogram, the column values are divided into bands so that each band contains approximately the same number of rows. The useful information that the histogram provides is where in the range of values the endpoints fall.

Frequency HistogramsIn a frequency histogram, each value of the column corresponds to a single bucket of the histogram. Each bucket contains the number of occurrences of that single value. Frequency histograms are automatically created instead of height-balanced histograms when the number of distinct values is less than or equal to the number of histogram buckets specified.

A test about histogram

SQL> create table hist_test(cola NUMBER, colb VARCHAR2(10));

表已创建。

SQL> begin
2 <>
3 for i in 1..20 loop
4 <>
5 for j in 1..i loop
6 insert into hist_test values(i,'TEST');
7 end loop inner;
8 end loop outer;
9 end;
10 /

PL/SQL 过程已成功完成。

SQL> select count(*) from hist_test;

COUNT(*)
----------
210

SQL> select dbms_stats.get_param('METHOD_OPT') from dual;

DBMS_STATS.GET_PARAM('METHOD_OPT')
------------------------------------
FOR ALL COLUMNS SIZE AUTO

SQL> exec dbms_stats.gather_table_stats(ownname=>'YCHEN',tabname=>'HIST_TEST');

PL/SQL 过程已成功完成。

SQL> SELECT column_name, num_distinct, num_buckets, histogram
2 FROM USER_TAB_COL_STATISTICS
3 WHERE table_name = 'HIST_TEST';

COLUM NUM_DISTINCT NUM_BUCKETS HISTOGRAM
----- ------------ ----------- ---------------
COLA 20 1 NONE
COLB 1 1 NONE

SQL> SELECT column_name, endpoint_number, endpoint_value
2 FROM USER_TAB_HISTOGRAMS
3 WHERE table_name= 'HIST_TEST'
4 ORDER BY column_name, endpoint_number;

COLUM ENDPOINT_NUMBER ENDPOINT_VALUE
----- --------------- --------------
COLA 0 1
COLA 1 20
COLB 0 4.3756E+35
COLB 1 4.3756E+35

SQL> exec dbms_stats.gather_table_stats(ownname=>'YCHEN',tabname=>'HIST_TEST',me
thod_opt=>'FOR COLUMNS SIZE 10 COLA');

PL/SQL 过程已成功完成。

SQL> SELECT column_name, num_distinct, num_buckets, histogram
2 FROM USER_TAB_COL_STATISTICS
3 WHERE table_name = 'HIST_TEST' and column_name = 'COLA';

COLUM NUM_DISTINCT NUM_BUCKETS HISTOGRAM
----- ------------ ----------- ---------------
COLA 20 10 HEIGHT BALANCED

SQL> SELECT column_name, endpoint_number, endpoint_value
2 FROM USER_TAB_HISTOGRAMS
3 WHERE table_name= 'HIST_TEST' and column_name = 'COLA'
4 ORDER BY column_name, endpoint_number;

COLUM ENDPOINT_NUMBER ENDPOINT_VALUE
----- --------------- --------------
COLA 0 1
COLA 1 7
COLA 2 9
COLA 3 11
COLA 4 13
COLA 5 15
COLA 6 16
COLA 7 17
COLA 8 18
COLA 9 19
COLA 10 20

已选择11行。

SQL> exec dbms_stats.gather_table_stats(ownname=>'YCHEN',tabname=>'HIST_TEST',me
thod_opt=>'FOR COLUMNS SIZE 30 COLA');

PL/SQL 过程已成功完成。

SQL> SELECT column_name, num_distinct, num_buckets, histogram
2 FROM USER_TAB_COL_STATISTICS
3 WHERE table_name = 'HIST_TEST' and column_name = 'COLA';

COLUM NUM_DISTINCT NUM_BUCKETS HISTOGRAM
----- ------------ ----------- ---------------
COLA 20 20 FREQUENCY

SQL> SELECT column_name, endpoint_number, endpoint_value
2 FROM USER_TAB_HISTOGRAMS
3 WHERE table_name= 'HIST_TEST' and column_name = 'COLA'
4 ORDER BY column_name, endpoint_number;

COLUM ENDPOINT_NUMBER ENDPOINT_VALUE
----- --------------- --------------
COLA 1 1
COLA 3 2
COLA 6 3
COLA 10 4
COLA 15 5
COLA 21 6
COLA 28 7
COLA 36 8
COLA 45 9
COLA 55 10
COLA 66 11
COLA 78 12
COLA 91 13
COLA 105 14
COLA 120 15
COLA 136 16
COLA 153 17
COLA 171 18
COLA 190 19
COLA 210 20

已选择20行。

[@more@]

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

转载于:http://blog.itpub.net/8558093/viewspace-1022264/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值