[20170612]FOR ALL COLUMNS SIZE repeat(12c).txt

--//昨天看了https://jonathanlewis.wordpress.com/2017/06/01/histogram-upgrade-2/,提到了直方图的问题,
--//特别是FOR ALL COLUMNS SIZE repeat引起的问题,在一些特殊情况要注意.

1.环境:
[email protected]> @ ver1
PORT_STRING                    VERSION        BANNER                                                                       CON_ID
------------------------------ -------------- ---------------------------------------------------------------------------- ------
IBMPC/WIN_NT64-9.1.0           12.1.0.1.0     Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production      0

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]> exec dbms_stats.Gather_table_stats(ownname => user, tabname => ‘T‘,method_opt => ‘FOR ALL COLUMNS ‘);
PL/SQL procedure successfully completed.

[email protected]> SELECT flag, COUNT(*) FROM t GROUP BY flag;
F   COUNT(*)
- ----------
d          2
a      20000

select * from DBA_TAB_COL_STATISTICS where owner=user and table_name=‘T‘ and column_name=‘FLAG‘;

Record View
As of: 2017/6/12 20:38:56

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 20:37:55
SAMPLE_SIZE:    20002
GLOBAL_STATS:   YES
USER_STATS:     NO
NOTES:          
AVG_COL_LEN:    2
HISTOGRAM:      FREQUENCY
SCOPE:          SHARED

--//建立了直方图.bucket=2.

[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 ENDPOINT_ACTUAL_VALU ENDPOINT_REPEAT_COUNT SCOPE
---------- ----------- --------------- ------------------------------------ ---------- -------------------- --------------------- -------
T          FLAG                  20000 503652795277878000000000000000000000                                                     0 SHARED
T          FLAG                  20002 519229685853483000000000000000000000                                                     0 SHARED
--//如果应用模式发生变化,加入了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.
--//加入No_Invalidate => false,主要使执行计划能马上使用新的统计信息.

select * from DBA_TAB_COL_STATISTICS where owner=user and table_name=‘T‘ and column_name=‘FLAG‘;

Record View
As of: 2017/6/12 20:42:29

OWNER:          SCOTT
TABLE_NAME:     T
COLUMN_NAME:    FLAG
NUM_DISTINCT:   4
LOW_VALUE:      61
HIGH_VALUE:     64
DENSITY:        0.25
NUM_NULLS:      0
NUM_BUCKETS:    2
LAST_ANALYZED:  2017/6/12 20:42:01
SAMPLE_SIZE:    5463
GLOBAL_STATS:   YES
USER_STATS:     NO
NOTES:          
AVG_COL_LEN:    2
HISTOGRAM:      HYBRID
SCOPE:          SHARED

--//因为bucket桶没有变化,直方图变成了HYBRID.

[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 ENDPOINT_ACTUAL_VALU ENDPOINT_REPEAT_COUNT SCOPE
---------- ----------- --------------- ------------------------------------ ---------- -------------------- --------------------- -------
T          FLAG                   2760 503652795277878000000000000000000000                                                  2760 SHARED
T          FLAG                   5463 519229685853483000000000000000000000                                                     1 SHARED

[email protected]> alter session set statistics_level=all;
Session altered.

[email protected]> SELECT flag, COUNT(*) FROM t GROUP BY flag order by 1;
F  COUNT(*)
- ---------
a     20000
b     20000
c         2
d         2

[email protected]> select * from t where flag=‘c‘;
   ID PAD                  F
----- -------------------- -
40003 cccccccccccccccccccc c
40004 cccccccccccccccccccc c

[email protected]> @ 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 |     205 |
|*  1 |  TABLE ACCESS FULL| T    |      1 |   6598 |   180K|    68   (0)| 00:00:01 |      2 |00:00:00.01 |     205 |
--------------------------------------------------------------------------------------------------------------------
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 存在很大的差距,正是业务模式发生了变化,导致method_opt => ‘FOR ALL COLUMNS size repeat‘存在问题.
--//而这个问题在11G是不存在的,在使用method_opt => ‘FOR ALL COLUMNS size repeat‘,会增加bucket的数量.
--//如果应用不存在这样的问题,估计关系不大.

--//简单的使用如下命令可以确定,"可能"有问题的直方图.
SELECT *
  FROM DBA_TAB_COL_STATISTICS
 WHERE     owner = USER
       AND table_name = ‘T‘
       AND num_distinct < 255
       AND histogram NOT LIKE ‘%FREQUENCY%‘;

Record View
As of: 2017/6/12 20:46:36

OWNER:          SCOTT
TABLE_NAME:     T
COLUMN_NAME:    FLAG
NUM_DISTINCT:   4
LOW_VALUE:      61
HIGH_VALUE:     64
DENSITY:        0.25
NUM_NULLS:      0
NUM_BUCKETS:    2
LAST_ANALYZED:  2017/6/12 20:42:01
SAMPLE_SIZE:    5463
GLOBAL_STATS:   YES
USER_STATS:     NO
NOTES:          
AVG_COL_LEN:    2
HISTOGRAM:      HYBRID
SCOPE:          SHARED

--//如果不指定repeat.
[email protected]> exec dbms_stats.Gather_table_stats(ownname => user, tabname => ‘T‘,method_opt => ‘FOR ALL COLUMNS ‘,No_Invalidate => false);
PL/SQL procedure successfully completed.

[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 ENDPOINT_ACTUAL_VALU ENDPOINT_REPEAT_COUNT SCOPE
---------- -------------------- --------------- ------------------------------------ ---------- -------------------- --------------------- -------
T          FLAG                           20000 503652795277878000000000000000000000                                                     0 SHARED
T          FLAG                           40000 508845092136413000000000000000000000                                                     0 SHARED
T          FLAG                           40002 514037388994948000000000000000000000                                                     0 SHARED
T          FLAG                           40004 519229685853483000000000000000000000                                                     0 SHARED
--//分析使用No_Invalidate => false,这样重新执行会重新分析.

select * from DBA_TAB_COL_STATISTICS where owner=user and table_name=‘T‘ and column_name=‘FLAG‘;

Record View
As of: 2017/6/12 20:49:27

OWNER:          SCOTT
TABLE_NAME:     T
COLUMN_NAME:    FLAG
NUM_DISTINCT:   4
LOW_VALUE:      61
HIGH_VALUE:     64
DENSITY:        0.0000124987501249875
NUM_NULLS:      0
NUM_BUCKETS:    4
LAST_ANALYZED:  2017/6/12 20:47:21
SAMPLE_SIZE:    40004
GLOBAL_STATS:   YES
USER_STATS:     NO
NOTES:          
AVG_COL_LEN:    2
HISTOGRAM:      FREQUENCY
SCOPE:          SHARED
--//这样建立的直方图=FREQUENCY.

[email protected]> select * from t where flag=‘c‘;
   ID PAD                  F
----- -------------------- -
40003 cccccccccccccccccccc c
40004 cccccccccccccccccccc c

[email protected]> @ 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 |     205 |
|*  1 |  TABLE ACCESS FULL| T    |      1 |      2 |    56 |    68   (0)| 00:00:01 |      2 |00:00:00.01 |     205 |
--------------------------------------------------------------------------------------------------------------------
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‘)

--//这样就ok了.
--//对于这样的情况最佳的方式手工设置分析方式

BEGIN
dbms_stats.Set_table_prefs(user, ‘T‘, ‘METHOD_OPT‘=>‘FOR ALL COLUMNS SIZE 1, FOR COLUMNS SIZE AUTO flag‘);
END;
/

--//避免这个问题. 总之12c要注意分析方法METHOD_OPT‘=>‘FOR ALL COLUMNS SIZE repeat‘带来的问题.
--//特别是一些应用模式发生变化的情况.而11g前面的测试不存在这个问题.

时间: 2024-11-08 03:23:37

[20170612]FOR ALL COLUMNS SIZE repeat(12c).txt的相关文章

[20170612]FOR ALL COLUMNS SIZE repeat(11g).txt

--//昨天看了https://jonathanlewis.wordpress.com/2017/06/01/histogram-upgrade-2/,提到了直方图的问题,--//特别是FOR ALL COLUMNS SIZE repeat引起的问题,在一些特殊情况要注意. 1.环境:[email protected]> @ &r/ver1 PORT_STRING                    VERSION        BANNER------------------------

[20180828]exadata--豆腐渣系统的保护神.txt

--//昨天看awr报表发现如下,时间8-9点报表,这个时间病房业务很少,主要门诊的业务: 1.awr报表情况:Top 10 Foreground Events by Total Wait Time Event                               Waits     Total Wait Time (sec)     Wait Avg(ms)     % DB time     Wait ClassDB CPU                               

【重磅干货】看了此文,Oracle SQL优化文章不必再看!

听“俊”一席话,胜读十年书.看了这篇由DBA+社群联合发起人丁俊大师(网名:dingjun123)分享的SQL优化大作,其他Oracle SQL优化文章都不必再看了! 专家简介 丁俊 网名:dingjun123 DBA+社群联合发起人 性能优化专家,Oracle ACEA,ITPUB开发版资深版主.8年电信行业从业经验,在某大型电信系统提供商工作7年,任资深工程师,从事过系统开发与维护.业务架构和数据分析.系统优化等工作.擅长基于ORACLE的系统优化,精通SQL.PL/SQL.JAVA等.电子

基于oracle的sql优化方法论

Oracle数据库里SQL优化的终极目标就是要缩短目标SQL语句的执行时间.要达到上述目的,我们通常只有如下三种方法可以选择: 1.降低目标SQL语句的资源消耗: 2.并行执行目标SQL语句: 3.平衡系统的资源消耗. "方法1:降低目标SQL语句的资源消耗"以缩短执行时间,这是最常用的SQL优化方法.这种方法的核心是要么通过在不更改业务逻辑的情况下改写SQL来降低目标SQL语句的资源消耗,要么不改SQL但通过调整执行计划或相关表的数据来降低目标SQL语句的资源消耗. 方法2:并行执行

oracle dbms_stats统计信息管理

---------------------------------------------------- 1.测试环境 ---------------------------------------------------- create table tt as select * from dba_objects; create index index_01 on tt(object_id); analyze index index_01 delete statistics; select co

4、统计信息

1.什么是统计信息? 统计信息就是数据库的侦察兵,记录了表和表中一些列的一些信息,一般SQL指定执行计划的时候都要先看表的统计信息,以决定执行计划的选择. 表的统计信息一般在dba_tables中,表中列的统计信息一般在dba_tab_col_statistics select table_name,column_name,num_distinct,num_nulls,last_analyzed,avg_col_len,histogram from dba_tab_col_statistics

9i oracle数据库迁移到11G

一.迁移前查询110库 活动的用户为以下几个用户(BOSS,MD_QZY,SMART,SRDQ,RIMS) 120库 活动的用户(BOSS,SMART) select distinct username from v$session; BOSSMD_QZYRIMSSMARTSRDQ 关闭监听,杀进程ps -ef |grep ora|awk '{print $2}'|xargs kill -9 查看迁移前的对象个数(不同用户查看):set lin 200 pages 100select owner

收集统计信息脚本

1.exec dbms_stats.FLUSH_DATABASE_MONITORING_INFO; For performance reasons, the Oracle Database does not populate these views immediately when the actual modifications occur. Run the FLUSH_DATABASE_MONITORING_INFO procedure in the DIMS_STATS PL/SQL pa

Oracle直方图的详细解析(转)

Oracle直方图解析 一.    何谓直方图: 直方图是一种统计学上的工具,并非Oracle专有.通常用于对被管理对象的某个方面的质量情况进行管理,通常情况下它会表现为一种几何图形表,这个图形表是根据从实际环境中所收集来的被管理对象某个方面的质量分布情况的数据所绘制成的,通常会画成以数量为底边,以频度为高度的一系列连接起来的矩形图,因此直方图在统计学上也称为质量分布图.比如下图所示,是一个以关学生化学考试成绩分数分布情况绘制的直方图:              二.       Oracle中