天天看點

DBMS_STATS.GATHER_SCHEMA_STATS介紹使用

dbms_stats能良好地估量統計資料(尤其是針對較大的分區表),并能取得更好的統計後果,最終制訂出速度更快的sql施行計劃。

  exec dbms_stats.gather_schema_stats(

  ownname          => 'scott',

  options          => 'gather auto',

  estimate_percent => dbms_stats.auto_sample_size,

  method_opt       => 'for all columns size repeat',

  degree           => 15

  )

為了充沛認識dbms_stats的益處,需要仔細領會每一條次要的預編譯指令(directive)。上面讓咱們鑽研每一條指令,并領會如何用它為基于代價的sql優化器搜羅最高品質的統計資料。

  options參數

  使用4個預設的法子之一,這個選項能把握oracle統計的重新整理方法:

  gather——重新剖析整個架構(schema)。

  gather empty——隻剖析目前還沒有統計的表。

  gather stale——隻重新剖析修改量超過10%的表(這些修改包含拔出、更新和删除)。

  gather auto——重新剖析以後沒有統計的對象,以及統計資料過期(變髒)的對象。注意,使用gather auto相似于組合使用gather stale和gather empty。

  注意,不論gather stale仍是gather auto,都請求進行監視。假如你施行一個alter table xxx monitoring指令,oracle會用dba_tab_modifications視圖來跟蹤發生發火變動的表。這樣一來,你就确實地知道,自從上 一次剖析統計資料以來,發生發火了多少次拔出、更新和删除操作。

  estimate_percent選項

  estimate_percent參數是一種比照新的設計,它答應oracle的dbms_stats在搜羅統計資料時,自動估量要采樣的一個segment的最佳百分比:

  estimate_percent => dbms_stats.auto_sample_size

  要考證自動統計采樣的準确性,你可檢視dba_tables sample_size列。一個有趣的地方是,在使用自動采樣時,oracle會為一個樣本尺寸挑選5到20的百分比。記住,統計資料品質越好,cbo做出的抉擇越好。

  method_opt選項

  method_opt:for table --隻統計表

  for all indexed columns --隻統計有索引的表列

  for all indexes --隻剖析統計相幹索引

  for all columns

  dbms_stats的method_opt參數尤其合适在表和索引資料發生發火變動時重新整理統計資料。method_opt參數也合适用于判斷哪些列需要直方圖(histograms)。

  某些情形下,索引内的各個值的散播會影響cbo是使用一個索引仍是施行一次全表掃描的決議計劃。例如,假如在where子句中指定的值的數量不合錯誤稱,全表掃描就顯得比索引走訪更經濟。

  假如你有一個高度歪斜的索引(某些值的行數不合錯誤稱),就可建立oracle直方圖統計。但在現實世界中,出現這種情形的機率相稱小。使用 cbo時,最罕見的過失之一就是在cbo統計中不用要地引入直方圖。根據經驗,隻需在列值請求必需修改施行計劃時,才應使用直方圖。

  為了智能地生成直方圖,oracle為dbms_stats準備了method_opt參數。在method_opt子句中,還有一些首要的新選項,包含skewonly,repeat和auto:

  method_opt=>'for all columns size skewonly'

  method_opt=>'for all columns size repeat'

  method_opt=>'for all columns size auto'

  skewonly選項會耗損大量處置時間,因為它要檢查每個索引中的每個列的值的散播情形。

  假如dbms_stat覺察一個索引的各個列散播得不均勻,就會為那個索引建立直方圖,輔助基于代價的sql優化器抉擇是進行索引走訪,仍是進行全表 掃描走訪。例如,在一個索引中,假設有一個列在50%的行中,如清單b所示,那麼為了檢索這些行,全表掃描的速度會快于索引掃描。

  --*************************************************************

  -- skewonly option—detailed analysis

  --

  -- use this method for a first-time analysis for skewed indexes

  -- this runs a long time because all indexes are examined

  begin

  dbms_stats.gather_schema_stats(

  method_opt       => 'for all columns size skewonly',

  degree           => 7

  );

  end;

  重新剖析統計資料時,使用repeat選項,重新剖析義務所耗費的資源就會少一些。使用repeat選項(清單c)時,隻會為現有的直方圖重新剖析索引,不再搜尋其餘直方圖機會。活期重新剖析統計資料時,你應當采用這種方法。

  -- repeat option - only reanalyze histograms for indexes

  -- that have histograms

  -- following the initial analysis, the weekly analysis

  -- job will use the “repeat” option. the repeat option

  -- tells dbms_stats that no indexes have changed, and

  -- it will only reanalyze histograms for

  -- indexes that have histograms.

  --**************************************************************

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

exec dbms_stats.gather_schema_stats(ownname=>'使用者名稱',estimate_percent=>100,cascade=> true, degree =>12);