Oracle的自動統計資訊不收集直方圖的資訊

n-lauren發表於2017-08-07

在oracle9i中,預設的統計資訊收集是不收集直方圖資訊的,也就是說預設的MOTHOD_OPT模式為FOR ALL COLUMNS SIZE 1

在10g開始,dbms_stats包中預設的METHOD_OPT做了調整,預設的METHOD_OPT值為FOR ALL COLUMNS SIZE AUTO

SQL> select * from v$version;
BANNER
—————————————————————-
Oracle Database 10g Enterprise Edition Release 10.2.0.5.064bi
PL/SQL Release 10.2.0.5.0 – Production
CORE 10.2.0.5.0 Production
TNS for Linux: Version 10.2.0.5.0 – Production
NLSRTL Version 10.2.0.5.0 – Production
 
SQL> select dbms_stats.get_param('method_opt') from dual; DBMS_STATS.GET_PARAM('METHOD_OPT') ——————————————————————– FOR ALL COLUMNS SIZE AUTO

這就說明,從10g開始,統計資訊收集中的直方圖部分,收集與否是有oracle自從判斷,從實際的使用來看,oracle的智慧判斷並不是100%正確,
oracle往往會大量的收集一些並不是必須的直方圖資訊,而有些直方圖資訊又會對查詢造成不必要的影響

由於我們簡單的對直方圖進行刪除後,oracle的自動統計資訊又會重新收集,所以我們需要採取一些必要的方法,來規避這個問題

10g中:

  • 解決方案
  1. 刪除表的統計資訊
  2. 手工收集標的統計資訊,不收集直方圖
  3. lock表的統計資訊
  4. 建立JOB手工收集統計資訊

11g中

在11g中,oracle對dbms_stats包新增了新功能,提供給我們進行修改,可以使用dbms_stats.set_table_prefs包

  • 刪除直方圖資訊:

dbms_stats.delete_column_stats procedure and setting the col_stat_type parameter to HISTOGRAM.

BEGIN dbms_stats.delete_column_stats(
ownname=>'SH', tabname=>'SALES', colname=>'PROD_ID', col_stat_type=>'HISTOGRAM'); END; Use the new dbms_stats.set_table_pref procedure to set a specific value for the method_opt parameter for the table effected by this problem. The following value for the method_opt parameter tells Oracle to continue to collect histograms as usual on all of the columns in the SALES table except for the PROD_ID column, which should never have a histogram created on it. BEGIN dbms_stats.set_table_prefs('SH', 'SALES','METHOD_OPT', 'FOR ALL COLUMNS SIZE AUTO, FOR COLUMNS SIZE 1 PROD_ID'); END;
/

The auto stats gathering job or your own statistics gathering commands will now use the table preference you set when it gathers statistics on this table and will no longer create a histogram on the ID column.

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

相關文章