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=:Popularnum_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
參考資料: