-
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:要分析表的拥有者
-
tabname:要分析的表名.
-
partname:分区的名字,只对分区表或分区索引有用.
-
estimate_percent:采样行的百分比,取值范围[0.000001,100],null为全部分析,不采样. 常量:DBMS_STATS.AUTO_SAMPLE_SIZE是默认值,由Oracle绝定最佳取采样值.
-
block_sapmple:是否用块采样代替行采样.
-
method_opt:决定histograms信息是怎样被统计的.method_opt的取值如下:
-
for all columns:统计所有列的histograms.
-
for all indexed columns:统计所有indexed列的histograms.
-
for all hidden columns:统计你看不到列的histograms
-
for columns <list> SIZE <N> | REPEAT | AUTO | SKEWONLY:统计指定列的histograms.N的取值范围[1,254]; REPEAT上次统计过的histograms;AUTO由oracle决定N的大小;SKEWONLY multiple end-points with the same value which is what we define by
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/28878983/viewspace-2137643/
,如需转载,请注明出处,否则将追究法律责任。