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
评论0
最新资源