--//昨天看了https://jonathanlewis.wordpress.com/2017/06/01/histogram-upgrade-2/,提到了直方图的问题,
--//特别是FOR ALL COLUMNS SIZE repeat引起的问题,在一些特殊情况要注意.
1.环境:
[email protected]> @ &r/ver1
PORT_STRING VERSION BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx 11.2.0.4.0 Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
create table t (id number,pad varchar2(20),flag varchar2(1));
insert into t select rownum,lpad(‘a‘,20,‘a‘) ,‘a‘ from dual connect by level<=2e4;
insert into t select rownum+2e4,lpad(‘d‘,20,‘d‘) ,‘d‘ from dual connect by level<=2;
commit ;
[email protected]> SELECT flag, COUNT(*) FROM t GROUP BY flag;
F COUNT(*)
- ----------
d 2
a 20000
[email protected]> exec dbms_stats.Gather_table_stats(ownname => user, tabname => ‘T‘,method_opt => ‘FOR ALL COLUMNS ‘);
PL/SQL procedure successfully completed.
select * from DBA_TAB_COL_STATISTICS where owner=user and table_name=‘T‘ and column_name=‘FLAG‘;
Record View
As of: 2017/6/12 9:35:03
OWNER: SCOTT
TABLE_NAME: T
COLUMN_NAME: FLAG
NUM_DISTINCT: 2
LOW_VALUE: 61
HIGH_VALUE: 64
DENSITY: 0.000024997500249975
NUM_NULLS: 0
NUM_BUCKETS: 2
LAST_ANALYZED: 2017/6/12 9:33:35
SAMPLE_SIZE: 20002
GLOBAL_STATS: YES
USER_STATS: NO
AVG_COL_LEN: 2
HISTOGRAM: FREQUENCY
--//建立了直方图.
[email protected]> set numw 36
[email protected]> select * from USER_TAB_HISTOGRAMS where column_name=‘FLAG‘;
TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER ENDPOINT_VALUE ENDPOINT_A
---------- ----------- --------------- ------------------------------------ ----------
T FLAG 20000 503652795277878000000000000000000000
T FLAG 20002 519229685853483000000000000000000000
--//如果应用模式发生变化,加入了2种值.
insert into t select rownum+2e4+2,lpad(‘b‘,20,‘b‘) ,‘b‘ from dual connect by level<=2e4;
insert into t select rownum+4e4+2,lpad(‘c‘,20,‘c‘) ,‘c‘ from dual connect by level<=2;
commit ;
[email protected]> exec dbms_stats.Gather_table_stats(ownname => user, tabname => ‘T‘,method_opt => ‘FOR ALL COLUMNS size repeat‘,no_invalidate=> false);
PL/SQL procedure successfully completed.
select * from DBA_TAB_COL_STATISTICS where owner=user and table_name=‘T‘ and column_name=‘FLAG‘;
Record View
As of: 2017/6/12 9:38:59
OWNER: SCOTT
TABLE_NAME: T
COLUMN_NAME: FLAG
NUM_DISTINCT: 4
LOW_VALUE: 61
HIGH_VALUE: 64
DENSITY: 0.0000126552145963607
NUM_NULLS: 0
NUM_BUCKETS: 4
LAST_ANALYZED: 2017/6/12 9:38:10
SAMPLE_SIZE: 5432
GLOBAL_STATS: YES
USER_STATS: NO
AVG_COL_LEN: 2
HISTOGRAM: FREQUENCY
--//因为bucket桶变成了4个,直方图依旧是FREQUENCY.
[email protected]> select * from USER_TAB_HISTOGRAMS where table_name=‘T‘ and column_name=‘FLAG‘;
TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER ENDPOINT_VALUE ENDPOINT_A
---------- ----------- --------------- ------------------------------------ ----------
T FLAG 2775 503652795277878000000000000000000000
T FLAG 5430 508845092136413000000000000000000000
T FLAG 5431 514037388994948000000000000000000000
T FLAG 5432 519229685853483000000000000000000000
[email protected]> alter session set statistics_level=all;
Session altered.
[email protected]> select * from t where flag=‘c‘;
ID PAD F
----- -------------------- -
40003 cccccccccccccccccccc c
40004 cccccccccccccccccccc c
[email protected]> @ &r/dpc ‘‘ ‘‘
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID g3pmd0h5vnw5r, child number 0
-------------------------------------
select * from t where flag=‘c‘
Plan hash value: 1601196873
--------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time | A-Rows | A-Time | Buffers |
--------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | | 68 (100)| | 2 |00:00:00.01 | 204 |
|* 1 | TABLE ACCESS FULL| T | 1 | 7 | 196 | 68 (0)| 00:00:01 | 2 |00:00:00.01 | 204 |
--------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$1 / [email protected]$1
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("FLAG"=‘c‘)
23 rows selected.
--//可以发现E-rows 与 A-rows 差距不大,而12c的情况就不同了.12c如果使用method_opt => ‘FOR ALL COLUMNS size repeat‘后,
--//bucket=2,直方图变成了混合型直方图.也就是如果升级12c,要注意分析方法method_opt => ‘FOR ALL COLUMNS size repeat‘带来的问题.
--//再写有点长,另外写看12c的测试.
--//当然如果数据模型没有发生变化,问题可以不考虑.