-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhybrid_hist_newdensity.sql
46 lines (44 loc) · 1.15 KB
/
hybrid_hist_newdensity.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
* hybrid_hist_newdensity.sql
*
* The query calculates the new density value for hybrid histograms
* Based on information in following blog posts:
* Mohamed Houri:
* http://allthingsoracle.com/12c-hybrid-histogram/
* Nenad Noveljic:
* http://nenadnoveljic.com/blog/density-calculation-hybrid-histograms
*
* Author: Nenad Noveljic
*
* Input:
* :table_name
* :column_name
*
* Output:
* NewDensity
*
*/
with pop_v as
(
select
sum(endpoint_repeat_count) sum_pop_ep_rp_count ,
count(*) pop_value_count
from
user_tab_histograms uth
,user_tab_col_statistics ucs
where
uth.table_name = ucs.table_name
and uth.column_name = ucs.column_name
and uth.table_name = :table_name
and uth.column_name = :column_name
and (uth.endpoint_repeat_count - ucs.sample_size/ucs.num_buckets) > 0
),
ucs_v as
( select num_distinct,sample_size from user_tab_col_statistics
where table_name = :table_name and column_name = :column_name
)
select
( 1 - sum_pop_ep_rp_count/sample_size)/(num_distinct - pop_value_count )
NewDensity
from pop_v,ucs_v
;