[20160516]統計分析引數method_opt.txt

lfree發表於2016-05-16

[20160516]統計分析引數method_opt.txt

--統計分析引數method_opt我個人感覺是最能折騰人的引數.我自己曾經在這個引數上栽過跟頭,透過例子來說明看看.

1.環境:
SCOTT@book> @ &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

--如果你看包定義,可以發現有趣的事情,比如預設定義:
DEFAULT_CASCADE          CONSTANT BOOLEAN  := null;
DEFAULT_DEGREE_VALUE     CONSTANT NUMBER   := 32766;
DEFAULT_ESTIMATE_PERCENT CONSTANT NUMBER   := 101;
DEFAULT_METHOD_OPT       CONSTANT VARCHAR2(1) := 'Z';
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DEFAULT_NO_INVALIDATE    CONSTANT BOOLEAN     := null;
DEFAULT_GRANULARITY      CONSTANT VARCHAR2(1) := 'Z';
DEFAULT_PUBLISH          CONSTANT BOOLEAN     := true;
DEFAULT_INCREMENTAL      CONSTANT BOOLEAN     := false;
DEFAULT_STALE_PERCENT    CONSTANT NUMBER      := 10;
DEFAULT_AUTOSTATS_TARGET CONSTANT VARCHAR2(1) := 'Z';
DEFAULT_STAT_CATEGORY    CONSTANT VARCHAR2(20) := 'OBJECT_STATS';

--可以發現預設定義實際上是'Z'.

2.建立一個表測試看看:
SCOTT@book> create table t as select * from dba_objects;
Table created.

SCOTT@book> execute sys.dbms_stats.gather_table_stats ( OwnName => user,TabName => 't',Method_Opt => 'Z',Cascade => True ,No_Invalidate => false)
PL/SQL procedure successfully completed.

SCOTT@book> select column_name, num_distinct, DENSITY,histogram,NUM_BUCKETS from dba_tab_columns where table_name='T' and owner=user;
COLUMN_NAME          NUM_DISTINCT    DENSITY HISTOGRAM       NUM_BUCKETS
-------------------- ------------ ---------- --------------- -----------
OWNER                          29 .034482759 NONE                      1
OBJECT_NAME                 52164  .00001917 NONE                      1
SUBOBJECT_NAME                149 .006711409 NONE                      1
OBJECT_ID                   86946 .000011501 NONE                      1
DATA_OBJECT_ID               9044 .000110571 NONE                      1
OBJECT_TYPE                    46  .02173913 NONE                      1
CREATED                      1036 .000965251 NONE                      1
LAST_DDL_TIME                1307 .000765111 NONE                      1
TIMESTAMP                    1336 .000748503 NONE                      1
STATUS                          2         .5 NONE                      1
TEMPORARY                       2         .5 NONE                      1
GENERATED                       2         .5 NONE                      1
SECONDARY                       2         .5 NONE                      1
NAMESPACE                      21 .047619048 NONE                      1
EDITION_NAME                    0          0 NONE                      0
15 rows selected.

SCOTT@book> select dbms_stats.GET_PARAM('METHOD_OPT') c30 from dual ;
C30
------------------------------
FOR ALL COLUMNS SIZE AUTO
--預設實際上是FOR ALL COLUMNS SIZE AUTO.順便做一個查詢包含全部欄位.

SCOTT@book> select * from t where owner=user and OBJECT_NAME='T' and SUBOBJECT_NAME='1' and OBJECT_ID=12 and DATA_OBJECT_ID=12 and OBJECT_TYPE='TABLE' and CREATED=sysdate;
no rows selected

SCOTT@book> execute dbms_stats.flush_database_monitoring_info
PL/SQL procedure successfully completed.

SCOTT@book> execute sys.dbms_stats.gather_table_stats ( OwnName => user,TabName => 't',Method_Opt => 'Z',Cascade => True ,No_Invalidate => false)
PL/SQL procedure successfully completed.

SCOTT@book> select column_name, num_distinct, DENSITY,histogram,NUM_BUCKETS from dba_tab_columns where table_name='T' and owner=user;
COLUMN_NAME          NUM_DISTINCT    DENSITY HISTOGRAM       NUM_BUCKETS
-------------------- ------------ ---------- --------------- -----------
OWNER                          29 5.6432E-06 FREQUENCY                23
OBJECT_NAME                 52164  .00001917 NONE                      1
SUBOBJECT_NAME                149  .00094518 FREQUENCY               149
OBJECT_ID                   86946 .000011501 NONE                      1
DATA_OBJECT_ID               9044 .000110571 NONE                      1
OBJECT_TYPE                    46 5.6432E-06 FREQUENCY                32
CREATED                      1036 .002824859 HEIGHT BALANCED         254
LAST_DDL_TIME                1307 .000765111 NONE                      1
TIMESTAMP                    1336 .000748503 NONE                      1
STATUS                          2         .5 NONE                      1
TEMPORARY                       2         .5 NONE                      1
GENERATED                       2         .5 NONE                      1
SECONDARY                       2         .5 NONE                      1
NAMESPACE                      21 .047619048 NONE                      1
EDITION_NAME                    0          0 NONE                      0
15 rows selected.

--估計'Z'=>'FOR ALL COLUMNS SIZE AUTO'.

3.最常見的錯誤,就是少寫ALL,寫成了Method_Opt => 'Method_Opt => 'FOR COLUMNS SIZE 1',這樣並不會刪除直方圖.

SCOTT@book> execute sys.dbms_stats.gather_table_stats ( OwnName => user,TabName => 't',Method_Opt => 'FOR COLUMNS SIZE 1',Cascade => True ,No_Invalidate => false)
PL/SQL procedure successfully completed.

SCOTT@book> select column_name, num_distinct, DENSITY,histogram,NUM_BUCKETS from dba_tab_columns where table_name='T' and owner=user;
COLUMN_NAME          NUM_DISTINCT    DENSITY HISTOGRAM       NUM_BUCKETS
-------------------- ------------ ---------- --------------- -----------
OWNER                          29 5.6432E-06 FREQUENCY                23
OBJECT_NAME                 52164  .00001917 NONE                      1
SUBOBJECT_NAME                149  .00094518 FREQUENCY               149
OBJECT_ID                   86946 .000011501 NONE                      1
DATA_OBJECT_ID               9044 .000110571 NONE                      1
OBJECT_TYPE                    46 5.6432E-06 FREQUENCY                32
CREATED                      1036 .002824859 HEIGHT BALANCED         254
LAST_DDL_TIME                1307 .000765111 NONE                      1
TIMESTAMP                    1336 .000748503 NONE                      1
STATUS                          2         .5 NONE                      1
TEMPORARY                       2         .5 NONE                      1
GENERATED                       2         .5 NONE                      1
SECONDARY                       2         .5 NONE                      1
NAMESPACE                      21 .047619048 NONE                      1
EDITION_NAME                    0          0 NONE                      0
15 rows selected.

--可以發現直方圖資訊不會刪除.

4.另外一個錯誤是寫成如下,例子:
Method_Opt => 'FOR COLUMNS owner,object_name SIZE 10'

SCOTT@book> execute sys.dbms_stats.gather_table_stats ( OwnName => user,TabName => 't',Method_Opt => 'FOR COLUMNS owner,object_name SIZE 10',Cascade => True ,No_Invalidate => false)
PL/SQL procedure successfully completed.

SCOTT@book> select column_name, num_distinct, DENSITY,histogram,NUM_BUCKETS from dba_tab_columns where table_name='T' and owner=user;
COLUMN_NAME          NUM_DISTINCT    DENSITY HISTOGRAM       NUM_BUCKETS
-------------------- ------------ ---------- --------------- -----------
OWNER                          29 5.7092E-06 FREQUENCY                24
OBJECT_NAME                 52164 .000031083 HEIGHT BALANCED          10
SUBOBJECT_NAME                149  .00094518 FREQUENCY               149
OBJECT_ID                   86946 .000011501 NONE                      1
DATA_OBJECT_ID               9044 .000110571 NONE                      1
OBJECT_TYPE                    46 5.6432E-06 FREQUENCY                32
CREATED                      1036 .002824859 HEIGHT BALANCED         254
LAST_DDL_TIME                1307 .000765111 NONE                      1
TIMESTAMP                    1336 .000748503 NONE                      1
STATUS                          2         .5 NONE                      1
TEMPORARY                       2         .5 NONE                      1
GENERATED                       2         .5 NONE                      1
SECONDARY                       2         .5 NONE                      1
NAMESPACE                      21 .047619048 NONE                      1
EDITION_NAME                    0          0 NONE                      0
15 rows selected.

--可以發現bucket的數量不對.寫成如下更加明確:
SCOTT@book> execute sys.dbms_stats.gather_table_stats ( OwnName => user,TabName => 't',Method_Opt => 'FOR COLUMNS owner,object_name SIZE 1',Cascade => True ,No_Invalidate => false)
PL/SQL procedure successfully completed.

SCOTT@book> select column_name, num_distinct, DENSITY,histogram,NUM_BUCKETS from dba_tab_columns where table_name='T' and owner=user;
COLUMN_NAME          NUM_DISTINCT    DENSITY HISTOGRAM       NUM_BUCKETS
-------------------- ------------ ---------- --------------- -----------
OWNER                          29 5.7486E-06 FREQUENCY                22
OBJECT_NAME                 52164  .00001917 NONE                      1
SUBOBJECT_NAME                149  .00094518 FREQUENCY               149
OBJECT_ID                   86946 .000011501 NONE                      1
DATA_OBJECT_ID               9044 .000110571 NONE                      1
OBJECT_TYPE                    46 5.6432E-06 FREQUENCY                32
CREATED                      1036 .002824859 HEIGHT BALANCED         254
LAST_DDL_TIME                1307 .000765111 NONE                      1
TIMESTAMP                    1336 .000748503 NONE                      1
STATUS                          2         .5 NONE                      1
TEMPORARY                       2         .5 NONE                      1
GENERATED                       2         .5 NONE                      1
SECONDARY                       2         .5 NONE                      1
NAMESPACE                      21 .047619048 NONE                      1
EDITION_NAME                    0          0 NONE                      0
15 rows selected.

--owner 欄位的bucket數量不是1.當然這種錯誤不常見,一般建立直方圖我會選擇254.
SCOTT@book> execute sys.dbms_stats.gather_table_stats ( OwnName => user,TabName => 't',Method_Opt => 'FOR COLUMNS owner,object_name SIZE 254',Cascade => True ,No_Invalidate => false)
PL/SQL procedure successfully completed.

SCOTT@book> select column_name, num_distinct, DENSITY,histogram,NUM_BUCKETS from dba_tab_columns where table_name='T' and owner=user;
COLUMN_NAME          NUM_DISTINCT    DENSITY HISTOGRAM       NUM_BUCKETS
-------------------- ------------ ---------- --------------- -----------
OWNER                          29 .000005758 FREQUENCY                23
OBJECT_NAME                 52164 .000031516 HEIGHT BALANCED         254
SUBOBJECT_NAME                149  .00094518 FREQUENCY               149
OBJECT_ID                   86946 .000011501 NONE                      1
DATA_OBJECT_ID               9044 .000110571 NONE                      1
OBJECT_TYPE                    46 5.6432E-06 FREQUENCY                32
CREATED                      1036 .002824859 HEIGHT BALANCED         254
LAST_DDL_TIME                1307 .000765111 NONE                      1
TIMESTAMP                    1336 .000748503 NONE                      1
STATUS                          2         .5 NONE                      1
TEMPORARY                       2         .5 NONE                      1
GENERATED                       2         .5 NONE                      1
SECONDARY                       2         .5 NONE                      1
NAMESPACE                      21 .047619048 NONE                      1
EDITION_NAME                    0          0 NONE                      0
15 rows selected.

5.比較正確的寫法是:

Method_Opt => 'FOR COLUMNS SIZE 10 owner, OBJECT_NAME'
或者
Method_Opt => 'FOR COLUMNS SIZE 10 owner for columnssize 10 OBJECT_NAME'

SCOTT@book> execute sys.dbms_stats.gather_table_stats ( OwnName => user,TabName => 't',Method_Opt =>  'FOR COLUMNS SIZE 10 owner , OBJECT_NAME',Cascade => True ,No_Invalidate => false)
PL/SQL procedure successfully completed.
或者
SCOTT@book> execute sys.dbms_stats.gather_table_stats ( OwnName => user,TabName => 't',Method_Opt => 'FOR COLUMNS SIZE 10 owner for columns size 10 OBJECT_NAME',Cascade => True ,No_Invalidate => false)
PL/SQL procedure successfully completed.

SCOTT@book> select column_name, num_distinct, DENSITY,histogram,NUM_BUCKETS from dba_tab_columns where table_name='T' and owner=user;
COLUMN_NAME          NUM_DISTINCT    DENSITY HISTOGRAM       NUM_BUCKETS
-------------------- ------------ ---------- --------------- -----------
OWNER                          29 .023255814 HEIGHT BALANCED          10
OBJECT_NAME                 52164 .000031943 HEIGHT BALANCED          10
SUBOBJECT_NAME                149  .00094518 FREQUENCY               149
OBJECT_ID                   86946 .000011501 NONE                      1
DATA_OBJECT_ID               9044 .000110571 NONE                      1
OBJECT_TYPE                    46 5.6432E-06 FREQUENCY                32
CREATED                      1036 .002824859 HEIGHT BALANCED         254
LAST_DDL_TIME                1307 .000765111 NONE                      1
TIMESTAMP                    1336 .000748503 NONE                      1
STATUS                          2         .5 NONE                      1
TEMPORARY                       2         .5 NONE                      1
GENERATED                       2         .5 NONE                      1
SECONDARY                       2         .5 NONE                      1
NAMESPACE                      21 .047619048 NONE                      1
EDITION_NAME                    0          0 NONE                      0
15 rows selected.

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-2100870/,如需轉載,請註明出處,否則將追究法律責任。

相關文章