oracel 学习系列

压力测试工具 http://www.blogjava.net/envoydada/archive/2011/03/15/346300.html

CBO 是基于成本的优化器

RBO 是基于规则的优化器

基于CBO的执行计划需要依赖于系统的统计信息,这些统计信息的类型种类随着DBMS产品或相同DBMS的不同版本会存在一定的差异.统计信息包括行数、数据块数、没给数据块中的平均行数、行的平均长度、每个列常数的种类、离散程度、列值中的NUll的个数、聚簇因子、索引的深度、最大最小值、叶块的数量、运行系统的IO或CPU的使用情况等多种因素。

优点:

     在执行计划的选择判断更具现实性

     通过统计信息来管理控制优化

     即使没有深刻理解优化器的工作原理,也仍然能够确保最优化的性能。

对不同表的监视信息ORACLE 可以通过 视图 USER_TAB_MODIFICATIONS 中获取。

ORACLE 中dbms_stats的使用 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;

       使用alter table xxx monitoring;命令来实现Oracle表监视时,需要使用dbms_stats中的auto选项。如清单D所示,auto选项根据数据分布以及应用程序访问列的方式(例如通过监视而确定的一个列的工作量)来创建直方图。使用method_opt=>’auto’类似于在dbms_stats的option参数中使用gather auto。

begin   dbms_stats.gather_schema_stats(      ownname          => 'SCOTT',       estimate_percent => dbms_stats.auto_sample_size,       method_opt       => 'for all columns size auto',       degree           => 7    ); end;

并行统计收集degree参数 Oracle推荐设置DBMS_STATS的DEGREE参数为DBMS_STATS.AUTO_DEGREE,该参数允许Oracle根据对象的大小和并行性初始化参数的设置选择恰当的并行度。 聚簇索引,域索引,位图连接索引不能并行收集。
 

如何使用dbms_stats分析统计信息? --创建统计信息历史保留表

sql> exec dbms_stats.create_stat_table(ownname => 'scott',stattab => 'stat_table') ; 

--导出整个scheme的统计信息

sql> exec dbms_stats.export_schema_stats(ownname => 'scott',stattab => 'stat_table') ; 

--分析scheme

Exec dbms_stats.gather_schema_stats(  ownname => 'scott'options => 'GATHER AUTO'estimate_percent => dbms_stats.auto_sample_size,  method_opt => 'for all indexed columns 'degree => 6 ) 

--分析表

sql> exec dbms_stats.gather_table_stats(ownname => 'scott',tabname => 'work_list',estimate_percent => 10,method_opt=> 'for all indexed columns') ; 

--分析索引

SQL> exec dbms_stats.gather_index_stats(ownname => 'crm2',indname => 'IDX_ADM_PERMISSION_PID_MID',estimate_percent => '10',degree => '4') ;

--如果发现执行计划走错,删除表的统计信息

SQL>dbms_stats.delete_table_stats(ownname => 'scott',tabname => 'work_list') ;

--导入表的历史统计信息

sql> exec dbms_stats.import_table_stats(ownname => 'scott',tabname => 'work_list',stattab => 'stat_table') ; 

--如果进行分析后,大部分表的执行计划都走错,需要导回整个scheme的统计信息

sql> exec dbms_stats.import_schema_stats(ownname => 'scott',stattab => 'stat_table');

--导入索引的统计信息

SQL> exec dbms_stats.import_index_stats(ownname => 'crm2',indname => 'IDX_ADM_PERMISSION_PID_MID',stattab => 'stat_table'

--检查是否导入成功

SQL> select table_name,num_rows,a.blocks,a.last_analyzed from all_tables a where a.table_name='WORK_LIST'

分析数据库(包括所有的用户对象和系统对象):gather_database_stats

分析用户所有的对象(包括表、索引、簇):gather_schema_stats

分析表:gather_table_stats

分析索引:gather_index_stats

删除数据库统计信息:delete_database_stats

删除用户方案统计信息:delete_schema_stats

删除表统计信息:delete_table_stats

删除索引统计信息:delete_index_stats

删除列统计信息:delete_column_stats

设置表统计信息:set_table_stats

设置索引统计信息:set_index_stats

设置列统计信息:set_column_stats

原文地址:https://www.cnblogs.com/liwenchaoCode/p/8252816.html