天天看點

ANALYZE 的用法

1、三大功能

搜集和删除索引、表和簇的統計資訊

驗證表、索引和簇的結構

鑒定表和簇和行遷移和行聯接

針對analyze的搜集和删除統計資訊功能而言,oracle推薦使用DBMS_STATS包來搜集優化資訊,DBMS_STATS可以并行的搜集

資訊,可以搜集分區表的全局資訊,進一步來說,按成本的優化器隻會使用DBMS_STATS包所統計出來的資訊。

2、先決條件

必須在你自己的方案中或有ANALYZE ANY system 的權限,

3、文法

ANALYZE

  { TABLE [ schema.]table

      [ PARTITION ( partition ) | SUBPARTITION ( subpartition ) ]

  | INDEX [ schema. ]index

  | CLUSTER [ schema. ]cluster

  }

  { COMPUTE [ SYSTEM ] STATISTICS [for_clause]

  | ESTIMATE [ SYSTEM ] STATISTICS [for_clause][SAMPLE integer { ROWS | PERCENT }]

  | validation_clauses

  | LIST CHAINED ROWS [ into_clause ]

  | DELETE [ SYSTEM ] STATISTICS

  } ;

INDEX index:對索引進行分析,分析的結果會放在USER_INDEXES, ALL_INDEXES,或 DBA_INDEXES中

分析的内容:

Depth of the index from its root block to its leaf blocks (BLEVEL)

Number of leaf blocks (LEAF_BLOCKS)

Number of distinct index values (DISTINCT_KEYS)

Average number of leaf blocks for each index value (AVG_LEAF_BLOCKS_PER_KEY)

Average number of data blocks for each index value (for an index on a table) (AVG_DATA_BLOCKS_PER_KEY)

Clustering factor (how well ordered the rows are about the indexed values) (CLUSTERING_FACTOR)

TABLE table:對表進行分析,分析的結果會放在USER_TABLES, ALL_TABLES, and DBA_TABLES表中,在分析表的時候,

oracle也會分析基于函數的index所引用的表達式

Number of rows (NUM_ROWS) *

Number of data blocks below the high water mark (that is, the number of data blocks that have been formatted

to receive data, regardless whether they currently contain data or are empty) (BLOCKS)

* Number of data blocks allocated to the table that have never been used (EMPTY_BLOCKS) Average available

free space in each data block in bytes (AVG_SPACE)

Number of chained rows (CHAIN_COUNT) Average row length, including the row's overhead, in bytes

(AVG_ROW_LEN)

分析表的限制

不可以分析資料字典表

不可以分析擴充表,但可以用DBMS_STATS來實作這個目的

不可以分析臨時表

不可以計算或估計下列字段類型

REFs, varrays, nested tables, LOBs (LOBs are not analyzed, they are skipped), LONGs, or object types.

PARTITION | SUBPARTITION:對分區表或索引進行分析

CLUSTER cluster:對簇進行分析,分析的結果會放在ALL_CLUSTERS, USER_CLUSTERS and DBA_CLUSTERS.

compute_statistics_clause

文法:COMPUTE [ SYSTEM ] STATISTICS [for_clause]

對分析對像進行精确的統計,然後把資訊存儲的資料字典中。可以選擇對表或對字段進行分析。

computed和estimated這兩種方式的統計資料都被優化器用來影響sql的執行計劃

如果指定system選項就隻統計系統産生的資訊

for_clause

FOR TABLE:隻統計表

FOR COLUMNS:隻統計某個字段

FOR ALL COLUMNS:統計所有字段

FOR ALL INDEXED COLUMNS:統計索引的所有字段

estimate_statistics_clause

ESTIMATE [ SYSTEM ] STATISTICS [for_clause][SAMPLE integer { ROWS | PERCENT }]

隻是對部分行做一個大概的統計。适用于大表

SAMPLE:指定具體統計多少行,如果忽略這個參數的話,oracle會預設為1064行

ROWS causes:行數 Oracle to sample integer rows of the table or cluster or integer entries from the index.

The integer must be at least 1.

PERCENT causes:百分數

validation_clauses

分析REF或是對像的結構

EG:ANALYZE TABLE employees VALIDATE STRUCTURE CASCADE;

ANALYZE TABLE customers VALIDATE REF UPDATE;