db2haodb
作者db2haodb·2013-04-08 00:47
数据库开发工程师·IGI

runstats & reorg & reorgchk

字数 4546阅读 1225评论 0赞 1
(1)runstats------------------------------------------------------------------------------------------
runstats(run statistics)用于手工更新统计信息, 以使当前的目录表空间中统计信息和实际数据的信息相一致,最终目的是有利于数据库优化器根据统计信息选择最优执行方案。
1.在runstats执行完之后,静态SQL查询并不会使用最新的统计信息,需要手工重新执行"BIND"以绑定。动态的SQL不需要,因为动态sql是在执行sql的时候加载统计信息的。
2.runstats用法:
 RUNSTATS ON TABLE IOV.OPP_HIS_FACT_HM on all columns; = RUNSTATS ON TABLE IOV.OPP_HIS_FACT_HM;
 RUNSTATS ON TABLE IOV.OPP_HIS_FACT_HM on columns(depNo, depName);   --对指定的列收集统计信息
 RUNSTATS ON TABLE IOV.OPP_HIS_FACT_HM on key columns;  --对表的所有索引字段进行收集统计信息,key columns指索引字段,若无索引则不收集任何统计信息;视图不适用该用法。
 RUNSTATS ON TABLE IOV.OPP_HIS_FACT_HM on key columns and columns(depNo, depName);
 RUNSTATS ON TABLE IOV.OPP_HIS_FACT_HM and indexes all; --收集表和索引上的统计信息,不包含分布统计信息。
 RUNSTATS ON TABLE IOV.OPP_HIS_FACT_HM and detailed indexes all; --收集表上的统计信息和索引上的详细统计信息,不包含分布统计信息。
 RUNSTATS ON TABLE IOV.OPP_HIS_FACT_HM for indexes all; 只收集所有索引统计信息(不收集表统计信息)
 RUNSTATS ON TABLE IOV.OPP_HIS_FACT_HM for indexes ind1, ind2, ind3; 只收集指定索引的统计信息
 
 DETAILED 详细统计信息,适用于相对较大的索引,不适用与视图
 SAMPLED 必须和DETAILED联用,可减少CPU和内存的消耗,提高运行效率。
 RUNSTATS ON TABLE IOV.OPP_HIS_FACT_HM AND SAMPLED DETAILED INDEXES ALL; --(以快速方式)收集表和该表的所有索引的详细统计信息
 
3.如果是对所有的表进行统计信息更新:reorgchk:
 db2 -v SELECT tbname,nleaf,nlevels,STATS_TIME FROM sysibm.sysindexes;
 db2 -v reorgchk update statistics on table all;
 db2 -v SELECT tbname,nleaf,nlevels,STATS_TIME FROM sysibm.sysindexes;
4.判断是否执行过runstats:
 SELECT tbname,nleaf,nlevels,STATS_TIME FROM sysibm.sysindexes; 若未执行过runstats,则nleaf,nlevels为 -1,且STATS_TIME为"-",若执行过了,则都有值。
5.加上 allow read access表示在runstats时其他用户可以读该表; 加上 allow write access(默认)表示其他用户可以读取或写入。
-------------------************
6.若要统计频率(frequency) 和 分位数(quantile) 统计信息应加上with distribution;   
 frequency如:car表中type为A的100个,为'B'的200个... ; quantile 如:汽车制造流程1--200步,处于step10的100个, step 20的300个....
 频率(frequency)统计信息的默认值由数据库配置参数num_freqvalues决定,默认值为10,建议取10--100;若设置为0,则不保留任何频率的统计信息。
 分位数(quantile) 统计信息由数据库配置参数num_quantiles决定,默认值为20,建议取20--50,若设为1 则不统计任何分位数统计信息。
 使用方法:
 RUNSTATS ON TABLE IOV.OPP_HIS_FACT_HM with distribution and indexes all; --使用数据库配置参数num_freqvalues和num_quantiles的配置值
 RUNSTATS ON TABLE IOV.OPP_HIS_FACT_HM with distribution DEFAULT NUM_FREQVALUES 20 NUM_QUANTILES 30; --为NUM_FREQVALUES和NUM_QUANTILES指定值。
 RUNSTATS ON TABLE IOV.OPP_HIS_FACT_HM with distribution on columns(colA NUM_QUANTILES, colB NUM_FREQVALUES 20 NUM_QUANTILES 30) DEFAULT NUM_FREQVALUES 0 NUM_QUANTILES 0 AND INDEXES ALL;
 -- 对于colB列NUM_FREQVALUES是 20, NUM_QUANTILES 是 30;  对于colA列则是用为该表指定的参数值,即后面的DEFAULT NUM_FREQVALUES 0 NUM_QUANTILES 0; 对于其他列也是DEFAULT NUM_FREQVALUES 0 NUM_QUANTILES 0,不包含任何统计信息。
 

7.什么时候需要运行runstats:
  当向表中装入了数据并创建了新的索引
  当用reorg命令重新组织表和索引时
  当存在大量影响表和索引的更新、删除和插入操作
  在绑定对性能要求很好的程序之前


(2)reorg---------------------------------------------------------------------------------------------
对碎片进行重组。
用法:reorg table tabName;
分区表索引重组: reorg index ind_name for table tab_name;

(3)reorgchk--------------------------------------------------------------------------------------------------
可以用reorgchk检查是否需要执行reorg,(reorgchk的结果如果有"*",则需要reorg,一般都会有);
用法:
 reorgchk update statistics on table tab_name;
(4)bind & rebind----------------------------------------------------------------------------------------------------------
第一次用bind ,然后用rebind
bind语法请参考info center
rebind语法:rebind [package] package_name
        或:db2rbind database_name -l logfile_name;
 
(5)runstats & reorg & reorgchk & rebind---------------------------------------------------------------------------------------------
综合reorg和runstats可以得出:
因为reorg的时间要比runstats的时间长的多,所以当发现表的效率低下的时候,可以先执行runstats以更新当前的数据统计信息,并重新绑定(bind)应用程序(主要是静态SQL);
如果这样还没有改善性能,则就要考虑reorg了。所以整体顺序为:
   runstats-->bind或rebind --(若不能改变性能)-->reorg-->runstats-->bind或rebind
   因为reorgchk会对指定的表进行RUNSTATS操作,所以第一步的runstats可以省略,故步骤一般可以:
   reorgchk-->reorg-->runstats-->bind或rebind
   如:
   reorgchk update statistics on table user;
   reorg table user;
   RUNSTATS ON TABLE user AND SAMPLED DETAILED INDEXES ALL;
   db2rbind IRMTDB -l db2rbind.out;
   或:
   RUNSTATS ON TABLE user AND SAMPLED DETAILED INDEXES ALL;
   reorg table user;
   RUNSTATS ON TABLE user AND SAMPLED DETAILED INDEXES ALL;
   db2rbind IRMTDB -l db2rbind.out;

如果觉得我的文章对您有用,请点赞。您的支持将鼓励我继续创作!

1

添加新评论0 条评论

Ctrl+Enter 发表

作者其他文章

相关文章

相关问题

相关资料

X社区推广