ITPub博客

首页 > 数据库 > Oracle > DBMS_STATS.GATHER_SCHEMA_STATS介绍使用

DBMS_STATS.GATHER_SCHEMA_STATS介绍使用

Oracle 作者:xypincle 时间:2017-03-16 18:02:35 0 删除 编辑

  1. dbms_stats能良好地估量统计数据(尤其是针对较大的分区表),并能取得更好的统计后果,最终制订出速度更快的SQL施行计划。
  2.   exec dbms_stats.gather_schema_stats(
  3.   ownname          => 'SCOTT',
  4.   options          => 'GATHER AUTO',
  5.   estimate_percent => dbms_stats.auto_sample_size,
  6.   method_opt       => 'for all columns size repeat',
  7.   degree           => 15
  8.   )      
  9. 为了充沛认识dbms_stats的益处,需要仔细领会每一条次要的预编译指令(directive)。上面让咱们钻研每一条指令,并领会如何用它为基于代价的SQL优化器搜罗最高品质的统计数据。
  10.   options参数
  11.   使用4个预设的法子之一,这个选项能把握Oracle统计的刷新方法:
  12.   gather——重新剖析整个架构(Schema)。
  13.   gather empty——只剖析目前还没有统计的表。
  14.   gather stale——只重新剖析修改量超过10%的表(这些修改包含拔出、更新和删除)。
  15.   gather auto——重新剖析以后没有统计的对象,以及统计数据过期(变脏)的对象。注意,使用gather auto相似于组合使用gather stale和gather empty。
  16.   注意,不论gather stale仍是gather auto,都请求进行监视。假如你施行一个alter table xxx monitoring命令,Oracle会用dba_tab_modifications视图来跟踪发生发火变动的表。这样一来,你就确实地知道,自从上一次剖析统计数据以来,发生发火了多少次拔出、更新和删除操作。

  17.   estimate_percent选项
  18.   estimate_percent参数是一种比照新的设计,它答应Oracle的dbms_stats在搜罗统计数据时,自动估量要采样的一个segment的最佳百分比:
  19.   estimate_percent => dbms_stats.auto_sample_size
  20.   要考证自动统计采样的准确性,你可检视dba_tables sample_size列。一个有趣的地方是,在使用自动采样时,Oracle会为一个样本尺寸挑选5到20的百分比。记住,统计数据品质越好,CBO做出的抉择越好。

  21.   method_opt选项
  22.   method_opt:for table --只统计表
  23.   for all indexed columns --只统计有索引的表列
  24.   for all indexes --只剖析统计相干索引
  25.   for all columns
  26.   dbms_stats的method_opt参数尤其合适在表和索引数据发生发火变动时刷新统计数据。method_opt参数也合适用于判断哪些列需要直方图(histograms)。
  27.   某些情形下,索引内的各个值的散播会影响CBO是使用一个索引仍是施行一次全表扫描的决议计划。例如,假如在where子句中指定的值的数量不合错误称,全表扫描就显得比索引走访更经济。
  28.   假如你有一个高度歪斜的索引(某些值的行数不合错误称),就可创建Oracle直方图统计。但在现实世界中,出现这种情形的机率相称小。使用 CBO时,最罕见的过失之一就是在CBO统计中不用要地引入直方图。根据经验,只需在列值请求必需修改施行计划时,才应使用直方图。
  29.   为了智能地生成直方图,Oracle为dbms_stats准备了method_opt参数。在method_opt子句中,还有一些首要的新选项,包含skewonly,repeat和auto:
  30.   method_opt=>'for all columns size skewonly'
  31.   method_opt=>'for all columns size repeat'
  32.   method_opt=>'for all columns size auto'

  33.   skewonly选项会耗损大量处置时间,因为它要检查每个索引中的每个列的值的散播情形。
  34.   假如dbms_stat觉察一个索引的各个列散播得不均匀,就会为那个索引创建直方图,辅助基于代价的SQL优化器抉择是进行索引走访,仍是进行全表扫描走访。例如,在一个索引中,假设有一个列在50%的行中,如清单B所示,那么为了检索这些行,全表扫描的速度会快于索引扫描。
  35.   --*************************************************************
  36.   -- SKEWONLY option—Detailed analysis
  37.   --
  38.   -- Use this method for a first-time analysis for skewed indexes
  39.   -- This runs a long time because all indexes are examined
  40.   --*************************************************************
  41.   begin
  42.   dbms_stats.gather_schema_stats(
  43.   ownname          => 'SCOTT',
  44.   estimate_percent => dbms_stats.auto_sample_size,
  45.   method_opt       => 'for all columns size skewonly',
  46.   degree           => 7
  47.   );
  48.   end;

  49.   重新剖析统计数据时,使用repeat选项,重新剖析义务所耗费的资源就会少一些。使用repeat选项(清单C)时,只会为现有的直方图重新剖析索引,不再搜寻其余直方图机会。活期重新剖析统计数据时,你应当采用这种方法。
  50.   --*************************************************************
  51.   -- REPEAT OPTION - Only reanalyze histograms for indexes
  52.   -- that have histograms
  53.   --
  54.   -- Following the initial analysis, the weekly analysis
  55.   -- job will use the “repeat” option. The repeat option
  56.   -- tells dbms_stats that no indexes have changed, and
  57.   -- it will only reanalyze histograms for
  58.   -- indexes that have histograms.
  59.   --**************************************************************
  60.   begin
  61.   dbms_stats.gather_schema_stats(
  62.   ownname          => 'SCOTT',
  63.   estimate_percent => dbms_stats.auto_sample_size,
  64.   method_opt       => 'for all columns size repeat',
  65.   degree           => 7
  66.   );
  67.   end;
  68. ----------------------------------------------------------------------------------------------------------------------------
  69. Exec dbms_stats.gather_schema_stats(ownname=>'用户名称',estimate_percent=>100,cascade=> TRUE, degree =>12);
  70. 含义解释 ownname:填写需要分析的用户(该用户下所有表都将被分析)
  71.               estimate_percent:分析抽样的力度
  72.               cascade:是否对索引进行分析
  73.               degree:并行处理的cpu数量

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/28878983/viewspace-2135492/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2013-05-11

  • 博文量
    208
  • 访问量
    212338