Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5424485
  • 博文数量: 348
  • 博客积分: 2173
  • 博客等级: 上尉
  • 技术积分: 7900
  • 用 户 组: 普通用户
  • 注册时间: 2011-08-24 17:26
个人简介

雄关漫道真如铁,而今迈步从头越。

文章存档

2022年(4)

2020年(6)

2019年(2)

2018年(2)

2017年(34)

2016年(49)

2015年(53)

2014年(47)

2013年(72)

2012年(79)

分类: DB2/Informix

2013-10-18 12:53:01

在业务系统中,数据库在运转到一定的时间,业务响应开始变得缓慢,此时DBA根据数据库优化引擎的建议将开始对表进行优化。
本文描述了如何进行合理的优化实施步骤(本文只涉及非24小时交易表的优化,24小时交易表建议使用db2的在线优化方案)。
创建表tabinfo存储待优化表的信息
db2 "create table tabinfo(tab1 char(10),tab2 char(10),tab3 char(10)) in cbodd01"              
2、根据业务人员的反馈拿到24小时交易表(tab_24.txt)
#表t01存储数据库中全部的表,t02存储24小时表
#将tab_all.txt文件中的表使用shell全部导入到t01表中
db2 connect to cbusdb >>/dev/null 2>&1
cat tab_all.txt|while read a
do
 if [ $a<>'' ];
 then
   db2 "insert into t01 values('${a}')"
 else
   exit 0
 fi
done
#将24小时表使用shell全部导入到t02表中:
db2 connect to cbusdb >>/dev/null 2>&1
cat tab_24.txt|while read a
do
 if [ $a<>'' ];
 then
   db2 "insert into t01 values('${a}')"
 else
   exit 0
 fi
done
3、使用except将非24小时表导出到tabnew.list
db2 -x "select * from t01 except all select * from t02" >tabnew.list
3、创建临时存储表t03存储非24小时表的最新的统计信息:
db2 "create table t03 like modi_rows in cbodd01"
4、创建gettab.sh脚本
功能:根据tablist_non24(tab.list剔除了30张新增表及bkpag开头的表)清单从modi_rows表查询所有非24小时表的最新的统计信息并转存到临时表t03上
###############################################################################################
#db2 connect to cbusdb >>/dev/null 2>&1
#cat tabnew.list|while read tabname
#do
#db2 -x "insert into t03 select row_date,TABNAME,row_num from modi_rows where TABNAME='${tabname}' order by row_date desc fetch first 1 rows only"
#done
###############################################################################################
5、按照降序将临时表t03的前40行数据转储到临时表t04(此步骤为了在做优化时,平衡数据量缩小并行优化的时间)
1)创建一个自增序列表t04:db2 "create table t04(id int generated always as identity (start with 1 increment by 1),ROW_DATE DATE,TABNAME CHAR(40),ROW_NUM INTEGER) in cbodd01"
2)插入数据:db2 "insert into t04(ROW_DATE,TABNAME,ROW_NUM) select * from t03 order by row_num desc fetch first 40 rows only"
3)将前40张大表的奇数行插入tabinfo的tab1列:
db2 "insert into tabinfo(tab1) select TABNAME from t04 where mod(to_number(id),2)=1"
4)将前40张大表的偶数行插入tabinfo的tab2列:
db2 "insert into tabinfo(tab2) select TABNAME from t04 where mod(to_number(id),2)=0"
5)将剩余的表插入tabinfo的tab3列
  首先:删除刚才查找的前40张大表
  db2 "delete from t03 where tabname in(select tabname from t03 order by row_num desc fetch first 40 rows only)"
  最后插入剩下的表:
  db2 "insert into tabinfo(tab3) select TABNAME from t03"
6、将上表每一列分别导出到t1.txt、t2.txt、t3.txt(也可以直接从表tabinfo中查找优化)
7、优化操作(同时启动3个shell将cat的文件名字修改成t1.txt\t2.txt\t3.txt)
db2 connect to cbusdb >>/dev/null 2>&1
cat t1.txt|while read a
do
 if [ $a<>'' ];
 then
   db2 "reorg table tabname use tmpsys" >>/dev/null 2>&1
   db2 "runstats on table tabname with distribution and detailed indexes all" >>/dev/null 2>&1
 else
   exit 0
 fi
done
到此完~如有问题欢迎随时交流
阅读(5096) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~