DBMS_STATS.GATHER_TABLE_STATS分析表詳解

作用:DBMS_STATS.GATHER_TABLE_STATS統計表,列,索引的統計信息.

DBMS_STATS.GATHER_TABLE_STATS的語法如下:

DBMS_STATS.GATHER_TABLE_STATS (   ownname          VARCHAR2,     tabname          VARCHAR2,     partname         VARCHAR2,   estimate_percent NUMBER,     block_sample     BOOLEAN,   method_opt       VARCHAR2,   degree           NUMBER,   granularity      VARCHAR2,     cascade          BOOLEAN,   stattab          VARCHAR2,     statid           VARCHAR2,   statown          VARCHAR2,   no_invalidate    BOOLEAN,   force            BOOLEAN);

參數說明:

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]; REPEAT上次統計過的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.

cascace:是收集索引的信息.默認爲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);

------------------------------------------------------------------------------------------------------------------------

自從Oracle8.1.5引入dbms_stats包,Experts們便推薦使用dbms_stats取代analyze。 理由如下

dbms_stats可以並行分析
dbms_stats有自動分析的功能(alter table monitor )
analyze 分析統計信息的不準確some times


1,2好理解,且第2點實際上在VLDB中是最吸引人的;3以前比較模糊,看了metalink236935.1 解釋,analyze在分析Partition表的時候,有時候會計算出不準確的Global statistics .

原因是,dbms_stats會實在的去分析表全局統計信息(當指定參數);而analyze是將表分區(局部)的statistics 彙總計算成表全局statistics ,可能導致誤差。

如果想分析整個用戶或數據庫,還可以採用工具包,可以並行分析
Dbms_utility(8i以前的工具包)
Dbms_stats(8i以後提供的工具包)

dbms_stats.gather_schema_stats(User,estimate_percent=>100,cascade=> TRUE);
dbms_stats.gather_table_stats(User,TableName,degree => 4,cascade => true);

這是對命令與工具包的一些總結

1、對於分區表,建議使用DBMS_STATS,而不是使用Analyze語句。
a) 可以並行進行,對多個用戶,多個Table
b) 可以得到整個分區表的數據和單個分區的數據。
c) 可以在不同級別上Compute Statistics:單個分區,子分區,全表,所有分區
d) 可以倒出統計信息
e) 可以用戶自動收集統計信息

2、DBMS_STATS的缺點
a) 不能Validate Structure
b) 不能收集CHAINED ROWS, 不能收集CLUSTER TABLE的信息,這兩個仍舊需要使用Analyze語句。
c) DBMS_STATS 默認不對索引進行Analyze,因爲默認Cascade是False,需要手工指定爲True

3、對於oracle 9裏面的External Table,Analyze不能使用,只能使用DBMS_STATS來收集信息。

-----------------------------------------------------------------
10G的文檔是這麼說的:
Do not use the COMPUTE and ESTIMATE clauses of ANALYZE to collect optimizer statistics. These clauses are supported for backward compatibility. Instead, use the DBMS_STATS package, which lets you collect statistics in parallel, collect global statistics for partitioned objects, and fine tune your statistics collection in other ways. The cost-based optimizer, which depends upon statistics, will eventually use only statistics that have been collected by DBMS_STATS

analyze的功能已經明確:
Use the ANALYZE statement (rather than DBMS_STATS) for statistics collection not related to the cost-based optimizer :

To use the VALIDATE or LIST CHAINED ROWS clauses

To collect information on freelist blocks

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章