1.业务背景
交易表存储了3个月交易数据,单表数据量达到了2亿条左右。此前为解决交易表数据量过大问题,已将联机交易表创建为分区
表,但随着交易量的增长,分区表也不能完全解决单表数据量过大的问题。为进一步解决交易表数据量过大,提升联机交易处理性
能,为当前表创建了历史表(也创建为分区表),且设定当前表仅保留15天数据,历史表保留2.5个月的数据。
2. 分区表数据移植步骤
在移植分区表数据之前,需要保证历史表和当前表的表结构完全一致(包含字段结构和索引)。
1) 从当前表detach需要移植的分区数据
alter table <table-name> detach partition <partition-name> into table <tmp-table-name>
2) 为临时表添加索引
为保证detach下来的临时表能快速的添加到历史表中,需要为临时表添加索引。(若不添加索引,将临时表添加到历史表时,历史表会重组整张表的索引,添加速度会很慢)。
创建索引命令如下:
create index <index-name> on <tmp-table-name>(<column-name> asc) allow reverse scans
3) 将临时表attach到历史表
alter table <table-name-his> attach partition <partition-name> starting('YYYYMMDD') inclusive ending('YYYYMMDD') inclusive
from table <tmp-table-name>
4) 重新创建当前表PART0000分区
PART0000分区为当前表的下边界分区,下边界分区为当前表的最小边界分区,因当前表detach了一个分区,需要重新创建
PART0000分区。创建命令如下:
alter table <table-name> add partition PART0000 starting(MINVALUE)
5)对当前表做RUNSTATS
对当前表做runstats,如果表数据量过大,可抽样做。
runstats on table <table-name> with distribution and sampled detailed indexes all tablesample system(10)
6) 如果当前表涉及到存储过程,需要设置存储过程生效并rebind
call sysproc.admin_revalidate_db_objects()
db2 rebind P1368868893
如果不重新绑定存储过程,可能会导致存储过程执行失败。
7) 对历史表数据做一致性检查
set integrity for <table-name-his> all immediate unchecked
8) 查看历史表分区可用性
db2 describe data partitions for table <table-name-his> show detail
9) 对历史表做runstats
runstats on table <table-name-his> with distribution and sampled detailed indexes all tablesample system(30)
indexsample system(50)