天天看點

Oracle 收集統計值 收集統計資訊

--收集Oracle統計資訊

Oracle 收集統計值 收集統計資訊

BEGIN

  DBMS_STATS.GATHER_TABLE_STATS(OWNNAME          => 'OWNER_NAME',  --資料庫使用者名

                                TABNAME          => 'TABLE_NAME',  --表名

                                PARTNAME        => 'PARTITION_NAME',  --分區名

                                ESTIMATE_PERCENT => DBMS_STATS.AUTO_SAMPLE_SIZE,

                                METHOD_OPT      => 'for all columns size repeat',

                                DEGREE          => DBMS_STATS.AUTO_DEGREE,

                                CASCADE          => TRUE);

END;

--查詢表的統計資訊

SELECT *

  FROM DBA_TAB_STATISTICS S

 WHERE S.OWNER = 'OWNER_NAME'  --資料庫使用者名

  AND S.TABLE_NAME = 'TABLE_NAME'  --表名

 ORDER BY S.LAST_ANALYZED DESC;

--Oracle統計資訊

DBMS_STATS.gather_table_stats

    (ownname varchar2,

    tabname varchar2,

    partname varchar2 default null,

    estimate_percent number default  to_estimate_percent_type(get_param('ESTIMATE_PERCENT')),

    block_sample boolean default FALSE,

    method_opt varchar2 default get_param('METHOD_OPT'),

    degree number default to_degree_type(get_param('DEGREE')),

    granularity varchar2 default get_param('GRANULARITY'),

    cascade boolean default to_cascade_type(get_param('CASCADE')),

    stattab varchar2 default null, statid varchar2 default null,

    statown varchar2 default null,

    no_invalidate boolean default to_no_invalidate_type(get_param('NO_INVALIDATE')),

    stattype varchar2 default 'DATA',

    force boolean default FALSE);

/*

參數說明:

ownname:  要分析表的擁有者

tabname:  要分析的表名.

partname: 分區的名字,隻對分區表或分區索引有用.

estimate_percent:采樣行的百分比,取值範圍[0.000001,100],null為全部分析,不采樣. 常量:DBMS_STATS.AUTO_SAMPLE_SIZE是預設值,由oracle決定最佳取采樣值.

block_sapmple:是否用塊采樣代替行采樣.

method_opt:    決定histograms資訊是怎樣被統計的.method_opt的取值如下:

for all columns:統計所有列的histograms.

for all indexed columns:統計所有indexed列的histograms.

for all hidden columns:統計你看不到列的histograms

for columns <list> SIZE <N> | REPEAT | AUTO | SKEWONLY:

                                              統計指定列的histograms.N的取值範圍[1,254]; R

                                              EPEAT上次統計過的histograms;

                                              AUTO由oracle決定N的大小;

                                              SKEWONLY multiple end-points with the same value which is what we define by "

                                                        there is skew in the data

degree:              設定收集統計資訊的并行度.預設值為null.

granularity:Granularity of statistics to collect ,only pertinent if the table is partitioned.

cascade:      是收集索引的資訊.預設為falase.

stattab        指定要存儲統計資訊的表,statid如果多個表的統計資訊存儲在同一個stattab中用于進行區分.statown存儲統計資訊表的擁有者.以上三個參數若不指定,

                    統計資訊會直接更新到資料字典.

no_invalidate: Does not invalidate the dependent cursors if set to TRUE. The procedure invalidates the dependent cursors 

                        immediately if set to FALSE.

force:        即使表鎖住了也收集統計資訊

*/

--例子:

execute dbms_stats.gather_table_stats(ownname => 'owner',

                                      tabname => 'table_name' ,

                                      estimate_percent => null ,

                                      method_opt => 'for all indexed columns' ,

                                      cascade => true);

--GATHER_INDEX_STATS

--==========================

SYS.DBMS_STATS.GATHER_INDEX_STATS (OwnName => 'ABC',

                                  IndName => 'IDX_FUNC_ABC',

                                  Estimate_Percent => 10,

                                  Degree => SYS.DBMS_STATS.DEFAULT_DEGREE,

                                  No_Invalidate => FALSE);