-
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(
-
ownname => 'SCOTT',
-
estimate_percent => dbms_stats.auto_sample_size,
-
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.
-
--**************************************************************
-
begin
-
dbms_stats.gather_schema_stats(
-
ownname => 'SCOTT',
-
estimate_percent => dbms_stats.auto_sample_size,
-
method_opt => 'for all columns size repeat',
-
degree => 7
-
);
-
end;
-
----------------------------------------------------------------------------------------------------------------------------
-
Exec dbms_stats.gather_schema_stats(ownname=>'用户名称',estimate_percent=>100,cascade=> TRUE, degree =>12);
-
含义解释 ownname:填写需要分析的用户(该用户下所有表都将被分析)
-
estimate_percent:分析抽样的力度
-
cascade:是否对索引进行分析
-
degree:并行处理的cpu数量
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/28878983/viewspace-2135492/
,如需转载,请注明出处,否则将追究法律责任。