什麼是統計資訊以及收集檢視方法

2021-06-26 18:28:39 字數 2751 閱讀 8967

概念:

oracle的統計資訊:儲存在資料字典裡,且從多個維度描述了oracle資料庫裡物件的詳細資訊。cbo會利用這些統計資訊來計算各條路徑的成本。

分類:

表、索引、列、系統、資料字典、內部物件的統計資訊

收集統計資訊:

analyze 命令和dbms_stats包。表、索引、列、

資料字典都可以用兩個

。系統、

內部物件只能用

dbms_stats。

1、analyze

analyze table proc_progress_log delete statistics; --刪除統計資訊

analyze table proc_progress_log estimate statistics sample 15 percent for table ; --估算模式,取樣比例15%(估算結果和實際結果不一定會完全匹配)

analyze table proc_progress_log compute statistics;--計算模式

select * from dba_tables where table_name = 'proc_progress_log'; --檢視表相關資訊

select * from user_tab_columns where table_name='proc_progress_log'--檢視列相關資訊

analyze table proc_smcs compute statistics for columns service_id , channel_id ; --對列進行計算模式的統計資訊收集

執行完成之後,

service_id , channel_id

確實已經有統計資訊了,但是

proc_smcs

表中的統計資訊將會被抹掉。

也就是說,對同乙個物件而言,新執行的

analyze

命令會抹掉之前的

analyze

的結果。

如果想一次性以計算模式收集表、表上的列、和表上的索引的統計資訊,執行:

analyze table proc_progress_log compute statistics;

用dbms_stats包收集統計資訊

官方推薦,oracle 8.1.5之後才有,看成是analyze的增強版

1、gather_table_stats:用於收集目標表、目標表的列和索引的統計資訊。

2、gather_index_stats:收集索引的。

3、gather_schema_stats:收集指定schema下的所有物件的。

4、gather_database_stats:收集全庫所有物件的。

exec dbms_stats.gather_table_stats(ownname => 'test',tabname=>'proc_letter',estimate_percent => 15,method_opt => 'for table',cascade => false);

只有proc_letter表有統計資訊,列和索引沒有。

method_opt => 'for table' 只適合11g以上版本,10以下的,還會收集列和索引的資訊。

如果採用計算模式,將estimate_percent => 15 設定成100或者null

exec dbms_stats.gather_table_stats(ownname => 'test',tabname=>'proc_letter',estimate_percent => 100,method_opt => 'for columns size 1 service_id channel_id',cascade => false);

以計算模式收集

service_id channel_id的統計資訊,同時

proc_letter表上也會有統計資訊。

dbms_stats做不到只收集列的統計資訊而不收集表的統計資訊。

exec dbms_stats.delete_table_stats(ownname => 'test' , tabname => 'proc_letter');

刪除統計資訊。

exec dbms_stats.gather_table_stats(ownname => 'test',tabname=>'proc_letter',estimate_percent => 100,cascade => false); 

一次性統計表、列、索引資訊。

analyze 和 dbms_stats的區別:1、

analyze 不能正確收集分割槽表的統計資訊,而

dbms_stats可以。

2、analyze 不能並行收集統計資訊,而

dbms_stats可以。

exec dbms_stats.gather_table_stats(ownname => 'test',tabname=>'proc_letter',estimate_percent => 100,cascade => false,degree=4); --並行度為4

3、dbms_stats不能收集與cbo無關的額外資訊,如行遷移/行鏈結的數量、校驗表和索引的結構資訊。

analyze table *** list chained rows into yyy --分析收集

行遷移/行鏈結的數量

analyze index *** validate structure 分析索引的結構

找出未收集統計資訊,以及統計資訊過期的表

下面這個查詢可以找到從未收集過統計資訊或者統計資訊過期的表。exec dbms stats.flush database monitoring info select owner,table name,object type,stale stats,last analyzed from dba ta...

如何 找出未收集統計資訊,以及統計資訊過期的表

下面這個查詢可以找到從未收集過統計資訊或者統計資訊過期的表。exec dbms stats.flush database monitoring info select owner,table name,object type,stale stats,last analyzed from dba ta...

如何 找出未收集統計資訊,以及統計資訊過期的表

下面這個查詢可以找到從未收集過統計資訊或者統計資訊過期的表。select owner,table name,object type,stale stats,last analyzed from dba tab statistics where stale stats yes or last anal...