num_rows: dba_tables.num_rows
num_nulls: dba_tab_cols.num_nulls
num_distinct:dba_tab_cols.num_distinct
Card:cardinality基数
oracle执行计划基数(cardinality)计算方式
1.单列无直方图计算方式
Card=(1/num_distinct)*(num_rows-num_nulls)/num_nulls
2.单列有直方图计算方式:
频率直方图: Bucketsize: 桶内的rowcount dba_tab_histograms.endpoint_value
Card :=Sum(Bucketsize)/num_rows
高度均衡直方图: popular value值基数计算方式:
PopValBucks:该Popular值的桶数 计算方式如下:
SELECT buckets PopValBucks from (
select endpoint_number, endpoint_value endstr, endpoint_number- lag( endpoint_number,1,0) over (order by endpoint_number )
as buckets from dba_tab_histograms where table_name=:table and column_name=:col )
where endstr=:Popular
num_buckets:总的桶数 DBA_TAB_cols.num_buckets
Card = num_rows * (PopValBucks /num_buckets) * ( (num_rows- num_nulls) / num_rows) ; 非popular value值基数计算方式:
Card =num_rows * Density * (num_rows-num_nulls)/num_nulls
附直方图收集方式:
EXECUTE DBMS_STATS.GATHER_TABLE_STATS
('scott','emp',METHOD_OPT => 'FOR COLUMNS SIZE auto ename');
Method_opt参数
EXECUTE DBMS_STATS.GATHER_TABLE_STATS
('scott','emp',METHOD_OPT => 'FOR COLUMNS SIZE 20 ename');
FOR ALL [INDEXED | HIDDEN] COLUMNS [size_clause]
FOR COLUMNS [size clause] column [size_clause] [,column...]
SIZE {integer | REPEAT | AUTO | SKEWONLY}
Integer – 人工指定直方图桶数 从1~254
REPEAT - 刷新现有的直方图列上的信息
AUTO - 基于数据分布和负载收集直方图
SKEWONLY- 基于数据分布收集直方图
?Size指定直方图的桶数
?若对直方图不甚了解,推荐使用AUTO或SKEWONLY
参考资料: