本文介绍了在生产环境下如何对一张大约存储了5亿条数据的24小时表进行清理的详细实施步骤和方法论。
一、在线全备CCBUABCD数据,争取最大化对外运营时间
db2 "export to CCBUABCD.del of del select * from CCBUABCD with ur"
二、通知应用工程师去掉程序可执行权限(申请对外停止业务10分钟)
三、数据清理与表结构变更
1、在线备份保留CCBUABCD数据
db2 "export to CCBUABCD.del of del select * from CCBUABCD where substr(cmevh_key,1,8) > '201309' with ur"
2、备份表结构
db2look -d cbusdb -e -t CCBUABCD -o CCBUABCD.sql
3、创建备表
db2 -tvf createtb.sql
createtb.sql内容:
-
CREATE TABLE "DB2INST1"."CCBUABCD_BAK" (
-
"CMEVH_LL" SMALLINT NOT NULL WITH DEFAULT 0 ,
-
"CMEVH_KEY" CHAR(27) NOT NULL WITH DEFAULT ' ' ,
-
"CMEVH_DB_TIMESTAMP" DECIMAL(15,0) NOT NULL WITH DEFAULT 0 ,
-
"CM_PROC_TLR_NO" CHAR(12) NOT NULL WITH DEFAULT ' ' ,
-
"CM_CHANNEL_FLAG" CHAR(2) NOT NULL WITH DEFAULT ' ' ,
-
"CM_TX_TYP" CHAR(1) NOT NULL WITH DEFAULT ' ' ,
-
"CM_EC_FLG" CHAR(1) NOT NULL WITH DEFAULT ' ' ,
-
"CM_EC_LOG_NO" CHAR(19) NOT NULL WITH DEFAULT ' ' ,
-
"CM_AR_LOG_NO" CHAR(19) NOT NULL WITH DEFAULT ' ' ,
-
"CM_TX_ID" CHAR(9) NOT NULL WITH DEFAULT ' ' ,
-
"CM_DRAWEE_NAME" CHAR(60) NOT NULL WITH DEFAULT ' ' ,
-
"CM_DRAWEE_ACCT_NO" CHAR(28) NOT NULL WITH DEFAULT ' ' ,
-
"CM_DRAWEE_ACCT_TYP" CHAR(1) NOT NULL WITH DEFAULT ' ' ,
-
"CM_DRAWEE_BK_NO" CHAR(12) NOT NULL WITH DEFAULT ' ' ,
-
"CM_PAYEE_NAME" CHAR(60) NOT NULL WITH DEFAULT ' ' ,
-
"CM_PAYEE_ACCT_NO" CHAR(28) NOT NULL WITH DEFAULT ' ' ,
-
"CM_PAYEE_ACCT_TYP" CHAR(1) NOT NULL WITH DEFAULT ' ' ,
-
"CM_PAYEE_BK_NO" CHAR(12) NOT NULL WITH DEFAULT ' ' ,
-
"CM_TX_AMT1" DECIMAL(15,2) NOT NULL WITH DEFAULT 0 ,
-
"CM_SVC" DECIMAL(15,2) NOT NULL WITH DEFAULT 0 ,
-
"CM_PSTG_EXP" DECIMAL(15,2) NOT NULL WITH DEFAULT 0 ,
-
"CM_TERM_LOG_NO" CHAR(19) NOT NULL WITH DEFAULT ' ' ,
-
"CM_PRINT_CNT" CHAR(2) NOT NULL WITH DEFAULT ' ' ,
-
"CM_PROC_TM" CHAR(6) NOT NULL WITH DEFAULT ' ' ,
-
"CM_REQ_DT" CHAR(8) NOT NULL WITH DEFAULT ' ' ,
-
"CM_PRINT_DT" CHAR(8) NOT NULL WITH DEFAULT ' ' ,
-
"CM_PRINT_TM" CHAR(6) NOT NULL WITH DEFAULT ' ' ,
-
"CM_OPR_ID" CHAR(12) NOT NULL WITH DEFAULT ' ' ,
-
"CM_SPV_ID" CHAR(12) NOT NULL WITH DEFAULT ' ' ,
-
"CM_DSCRP" CHAR(120) NOT NULL WITH DEFAULT ' ' ,
-
"CM_FILLER" CHAR(30) NOT NULL WITH DEFAULT ' ' ,
-
"BCMEVHY1X" CHAR(39) GENERATED ALWAYS AS (CM_CHANNEL_FLAG||CM_DRAWEE_ACCT_TYP||SUBSTR(CM_DRAWEE_BK_NO,1,9)||CMEVH_KEY) ,
-
"BCMEVHY2X" CHAR(39) GENERATED ALWAYS AS (CM_CHANNEL_FLAG||CM_PAYEE_ACCT_TYP||SUBSTR(CM_PAYEE_BK_NO,1,9)||CMEVH_KEY) ,
-
"BCMEVHY3X" CHAR(57) GENERATED ALWAYS AS (CM_CHANNEL_FLAG||CM_DRAWEE_ACCT_NO||CMEVH_KEY) ,
-
"BCMEVHY4X" CHAR(57) GENERATED ALWAYS AS (CM_CHANNEL_FLAG||CM_PAYEE_ACCT_NO||CMEVH_KEY) ,
-
"BCMEVHY5X" CHAR(27) GENERATED ALWAYS AS (SUBSTR(CMEVH_KEY,1,8)||CM_TERM_LOG_NO) ,
-
"BCMEVHY6X" CHAR(27) GENERATED ALWAYS AS (CM_TERM_LOG_NO||CM_REQ_DT) )
-
COMPRESS YES
-
INDEX IN "CBODX01" PARTITION BY RANGE("CM_PROC_TLR_NO")
-
(PART "100" STARTING('100') ENDING('200') EXCLUSIVE IN "CBOD_P_100",
-
PART "200" STARTING('200') ENDING('300') EXCLUSIVE IN "CBOD_P_200",
-
PART "300" STARTING('300') ENDING('400') EXCLUSIVE IN "CBOD_P_300",
-
PART "500" STARTING('500') ENDING('600') EXCLUSIVE IN "CBOD_P_500",
-
PART "600" STARTING('600') ENDING('700') EXCLUSIVE IN "CBOD_P_60",
-
PART "800" STARTING('800') ENDING('801') EXCLUSIVE IN "CBOD_P1_800",
-
PART "801" STARTING('801') ENDING('802') EXCLUSIVE IN "CBOD_P1_801",
-
PART "802" STARTING('802') ENDING('803') EXCLUSIVE IN "CBOD_P1_802",
-
PART "803" STARTING('803') ENDING('804') EXCLUSIVE IN "CBOD_P1_803",
-
PART "806" STARTING('806') ENDING('807') EXCLUSIVE IN "CBOD_P_806",
-
PART "808" STARTING('808') ENDING('809') EXCLUSIVE IN "CBOD_P_808",
-
PART "809" STARTING('809') ENDING('810') EXCLUSIVE IN "CBOD_P_809",
-
PART "810" STARTING('810') ENDING('811') EXCLUSIVE IN "CBOD_P_810",
-
PART "811" STARTING('811') ENDING('812') EXCLUSIVE IN "CBOD_P_811",
-
PART "812" STARTING('812') ENDING('813') EXCLUSIVE IN "CBOD_P_812",
-
PART "815" STARTING('815') ENDING('816') EXCLUSIVE IN "CBOD_P_815",
-
PART "816" STARTING('816') ENDING('817') EXCLUSIVE IN "CBOD_P_816",
-
PART "817" STARTING('817') ENDING('818') EXCLUSIVE IN "CBOD_P_817",
-
PART "818" STARTING('818') ENDING('819') EXCLUSIVE IN "CBOD_P_818",
-
PART "820" STARTING('820') ENDING('821') EXCLUSIVE IN "CBOD_P_820");
-
ALTER TABLE "DB2INST1"."CCBUABCD_BAK" VOLATILE CARDINALITY;
-
COMMIT WORK;
-
CONNECT RESET;
-
TERMINATE;
4、将保留数据备份到备表
db2 -tvf restore.sql
restore.sql内容如下所示:
-
insert into CCBUABCD_bak (
-
CMEVH_LL,
-
CMEVH_KEY,
-
CMEVH_DB_TIMESTAMP,
-
CM_PROC_TLR_NO,
-
CM_CHANNEL_FLAG,
-
CM_TX_TYP,
-
CM_EC_FLG,
-
CM_EC_LOG_NO,
-
CM_AR_LOG_NO,
-
CM_TX_ID,
-
CM_DRAWEE_NAME,
-
CM_DRAWEE_ACCT_NO,
-
CM_DRAWEE_ACCT_TYP,
-
CM_DRAWEE_BK_NO,
-
CM_PAYEE_NAME,
-
CM_PAYEE_ACCT_NO,
-
CM_PAYEE_ACCT_TYP,
-
CM_PAYEE_BK_NO,
-
CM_TX_AMT1,
-
CM_SVC,
-
CM_PSTG_EXP,
-
CM_TERM_LOG_NO,
-
CM_PRINT_CNT,
-
CM_PROC_TM,
-
CM_REQ_DT,
-
CM_PRINT_DT,
-
CM_PRINT_TM,
-
CM_OPR_ID,
-
CM_SPV_ID,
-
CM_DSCRP,
-
CM_FILLER
-
) select
-
CMEVH_LL,
-
CMEVH_KEY,
-
CMEVH_DB_TIMESTAMP,
-
CM_PROC_TLR_NO,
-
CM_CHANNEL_FLAG,
-
CM_TX_TYP,
-
CM_EC_FLG,
-
CM_EC_LOG_NO,
-
CM_AR_LOG_NO,
-
CM_TX_ID,
-
CM_DRAWEE_NAME,
-
CM_DRAWEE_ACCT_NO,
-
CM_DRAWEE_ACCT_TYP,
-
CM_DRAWEE_BK_NO,
-
CM_PAYEE_NAME,
-
CM_PAYEE_ACCT_NO,
-
CM_PAYEE_ACCT_TYP,
-
CM_PAYEE_BK_NO,
-
CM_TX_AMT1,
-
CM_SVC,
-
CM_PSTG_EXP,
-
CM_TERM_LOG_NO,
-
CM_PRINT_CNT,
-
CM_PROC_TM,
-
CM_REQ_DT,
-
CM_PRINT_DT,
-
CM_PRINT_TM,
-
CM_OPR_ID,
-
CM_SPV_ID,
-
CM_DSCRP,
-
CM_FILLER
-
from CCBUABCD where substr(cmevh_key,1,8) > '201309';
5、验证备表数据与原表中需要保留的数据的数量是否一致
db2 "select count(*) from CCBUABCD_bak"
db2 "select count(*) from CCBUABCD where substr(cmevh_key,1,8) > '201309'
6、删除原表
db2 "drop table CCBUABCD"
7、新建CCBUABCD表
db2 -stvf newtab.sql
newtab.sql内容如下所示:
-
CREATE TABLE DB2INST1.CCBUABCD
-
(
-
CMEVH_LL smallint not null default 0,
-
CMEVH_KEY char(27) not null default ' ',
-
CMEVH_DB_TIMESTAMP decimal(15) not null default 0,
-
CM_PROC_TLR_NO char(12) not null default ' ',
-
CM_CHANNEL_FLAG char(2) not null default ' ',
-
CM_TX_TYP char(1) not null default ' ',
-
CM_EC_FLG char(1) not null default ' ',
-
CM_EC_LOG_NO char(19) not null default ' ',
-
CM_AR_LOG_NO char(19) not null default ' ',
-
CM_TX_ID char(9) not null default ' ',
-
CM_DRAWEE_NAME char(60) not null default ' ',
-
CM_DRAWEE_ACCT_NO char(28) not null default ' ',
-
CM_DRAWEE_ACCT_TYP char(1) not null default ' ',
-
CM_DRAWEE_BK_NO char(12) not null default ' ',
-
CM_PAYEE_NAME char(60) not null default ' ',
-
CM_PAYEE_ACCT_NO char(28) not null default ' ',
-
CM_PAYEE_ACCT_TYP char(1) not null default ' ',
-
CM_PAYEE_BK_NO char(12) not null default ' ',
-
CM_TX_AMT1 decimal(15,2) not null default 0,
-
CM_SVC decimal(15,2) not null default 0,
-
CM_PSTG_EXP decimal(15,2) not null default 0,
-
CM_TERM_LOG_NO char(19) not null default ' ',
-
CM_PRINT_CNT char(2) not null default ' ',
-
CM_PROC_TM char(6) not null default ' ',
-
CM_REQ_DT char(8) not null default ' ',
-
CM_PRINT_DT char(8) not null default ' ',
-
CM_PRINT_TM char(6) not null default ' ',
-
CM_OPR_ID char(12) not null default ' ',
-
CM_SPV_ID char(12) not null default ' ',
-
CM_DSCRP char(120) not null default ' ',
-
CM_TX_MODE char(1) not null default ' ',
-
CM_EXTER_SYS_DT char(8) not null default ' ',
-
CM_EXTER_SYS_ID char(4) not null default ' ',
-
CM_EXTER_SUB_SYS_ID char(4) not null default ' ',
-
CM_FILLER char(30) not null default ' ',
-
BCMEVHY1X CHAR(45) generated always as (CM_CHANNEL_FLAG||CM_DRAWEE_ACCT_TYP||SUBSTR(CM_DRAWEE_BK_NO,1,9)||SUBSTR(CMEVH_KEY,1,8)||CM_PROC_TM||SUBSTR(CMEVH_KEY,9,19)),
-
BCMEVHY2X CHAR(45) generated always as (CM_CHANNEL_FLAG||CM_PAYEE_ACCT_TYP||SUBSTR(CM_PAYEE_BK_NO,1,9)||SUBSTR(CMEVH_KEY,1,8)||CM_PROC_TM||SUBSTR(CMEVH_KEY,9,19)),
-
BCMEVHY3X CHAR(63) generated always as (CM_CHANNEL_FLAG||CM_DRAWEE_ACCT_NO||SUBSTR(CMEVH_KEY,1,8)||CM_PROC_TM||SUBSTR(CMEVH_KEY,9,19)),
-
BCMEVHY4X CHAR(63) generated always as (CM_CHANNEL_FLAG||CM_PAYEE_ACCT_NO||SUBSTR(CMEVH_KEY,1,8)||CM_PROC_TM||SUBSTR(CMEVH_KEY,9,19)),
-
BCMEVHY5X CHAR(27) generated always as (SUBSTR(CMEVH_KEY,1,8)||CM_TERM_LOG_NO),
-
BCMEVHY6X CHAR(27) generated always as (CM_TERM_LOG_NO||CM_REQ_DT),
-
BCMEVHY7X CHAR(43) generated always as (CM_EXTER_SYS_ID||CM_EXTER_SUB_SYS_ID||CM_EXTER_SYS_DT||CMEVH_KEY),
-
BCMEVHY8X CHAR(39) generated always as (CM_EXTER_SYS_ID||CM_EXTER_SYS_DT||CMEVH_KEY),
-
BCMEVHY9X CHAR(37) generated always as (CM_CHANNEL_FLAG||CM_EXTER_SYS_DT||CMEVH_KEY),
-
BCMEVHYAX CHAR(35) generated always as (CM_EXTER_SYS_ID||CM_EXTER_SUB_SYS_ID||CMEVH_KEY),
-
BCMEVHYBX CHAR(29) generated always as (CM_CHANNEL_FLAG||CMEVH_KEY),
-
BCMEVHYCX CHAR(27) generated always as (CM_EXTER_SYS_DT||CM_TERM_LOG_NO),
-
BCMEVHYDX CHAR(45) generated always as (CM_EXTER_SYS_ID||CM_EXTER_SUB_SYS_ID||CM_CHANNEL_FLAG||CM_EXTER_SYS_DT||CMEVH_KEY)
-
) compress yes INDEX IN "CBODPX01" PARTITION BY RANGE("CM_PROC_TLR_NO")
-
(PART "100" STARTING('100') ENDING('200') EXCLUSIVE IN "CBOD_P_100",
-
PART "200" STARTING('200') ENDING('300') EXCLUSIVE IN "CBOD_P_200",
-
PART "300" STARTING('300') ENDING('400') EXCLUSIVE IN "CBOD_P_300",
-
PART "500" STARTING('500') ENDING('600') EXCLUSIVE IN "CBOD_P_500",
-
PART "600" STARTING('600') ENDING('700') EXCLUSIVE IN "CBOD_P_60",
-
PART "800" STARTING('800') ENDING('801') EXCLUSIVE IN "CBOD_P1_800",
-
PART "801" STARTING('801') ENDING('802') EXCLUSIVE IN "CBOD_P1_801",
-
PART "802" STARTING('802') ENDING('803') EXCLUSIVE IN "CBOD_P1_802",
-
PART "803" STARTING('803') ENDING('804') EXCLUSIVE IN "CBOD_P1_803",
-
PART "806" STARTING('806') ENDING('807') EXCLUSIVE IN "CBOD_P_806",
-
PART "808" STARTING('808') ENDING('809') EXCLUSIVE IN "CBOD_P_808",
-
PART "809" STARTING('809') ENDING('810') EXCLUSIVE IN "CBOD_P_809",
-
PART "810" STARTING('810') ENDING('811') EXCLUSIVE IN "CBOD_P_810",
-
PART "811" STARTING('811') ENDING('812') EXCLUSIVE IN "CBOD_P_811",
-
PART "812" STARTING('812') ENDING('813') EXCLUSIVE IN "CBOD_P_812",
-
PART "815" STARTING('815') ENDING('816') EXCLUSIVE IN "CBOD_P_815",
-
PART "816" STARTING('816') ENDING('817') EXCLUSIVE IN "CBOD_P_816",
-
PART "817" STARTING('817') ENDING('818') EXCLUSIVE IN "CBOD_P_817",
-
PART "818" STARTING('818') ENDING('819') EXCLUSIVE IN "CBOD_P_818",
-
PART "820" STARTING('820') ENDING('821') EXCLUSIVE IN "CBOD_P_820");
8、导入数据
db2 -tvf restorenew.sql
restorenew.sql内容:
-
insert into CCBUABCD (
-
CMEVH_LL,
-
CMEVH_KEY,
-
CMEVH_DB_TIMESTAMP,
-
CM_PROC_TLR_NO,
-
CM_CHANNEL_FLAG,
-
CM_TX_TYP,
-
CM_EC_FLG,
-
CM_EC_LOG_NO,
-
CM_AR_LOG_NO,
-
CM_TX_ID,
-
CM_DRAWEE_NAME,
-
CM_DRAWEE_ACCT_NO,
-
CM_DRAWEE_ACCT_TYP,
-
CM_DRAWEE_BK_NO,
-
CM_PAYEE_NAME,
-
CM_PAYEE_ACCT_NO,
-
CM_PAYEE_ACCT_TYP,
-
CM_PAYEE_BK_NO,
-
CM_TX_AMT1,
-
CM_SVC,
-
CM_PSTG_EXP,
-
CM_TERM_LOG_NO,
-
CM_PRINT_CNT,
-
CM_PROC_TM,
-
CM_REQ_DT,
-
CM_PRINT_DT,
-
CM_PRINT_TM,
-
CM_OPR_ID,
-
CM_SPV_ID,
-
CM_DSCRP,
-
CM_FILLER
-
) select
-
CMEVH_LL,
-
CMEVH_KEY,
-
CMEVH_DB_TIMESTAMP,
-
CM_PROC_TLR_NO,
-
CM_CHANNEL_FLAG,
-
CM_TX_TYP,
-
CM_EC_FLG,
-
CM_EC_LOG_NO,
-
CM_AR_LOG_NO,
-
CM_TX_ID,
-
CM_DRAWEE_NAME,
-
CM_DRAWEE_ACCT_NO,
-
CM_DRAWEE_ACCT_TYP,
-
CM_DRAWEE_BK_NO,
-
CM_PAYEE_NAME,
-
CM_PAYEE_ACCT_NO,
-
CM_PAYEE_ACCT_TYP,
-
CM_PAYEE_BK_NO,
-
CM_TX_AMT1,
-
CM_SVC,
-
CM_PSTG_EXP,
-
CM_TERM_LOG_NO,
-
CM_PRINT_CNT,
-
CM_PROC_TM,
-
CM_REQ_DT,
-
CM_PRINT_DT,
-
CM_PRINT_TM,
-
CM_OPR_ID,
-
CM_SPV_ID,
-
CM_DSCRP,
-
CM_FILLER
-
from CCBUABCD_BAK;
-
9、添加索引、主键等
-
db2 -tvf createidx.idx
-
createidx.idx内容如下:
-
ALTER TABLE DB2INST1.CCBUABCD VOLATILE CARDINALITY;
-
alter table DB2INST1.CCBUABCD
-
add primary key (CMEVH_KEY);
-
CREATE INDEX DB2INST1.BCMEVHY1
-
ON DB2INST1.CCBUABCD(BCMEVHY1X);
-
CREATE INDEX DB2INST1.BCMEVHY2
-
ON DB2INST1.CCBUABCD(BCMEVHY2X);
-
CREATE INDEX DB2INST1.BCMEVHY3
-
ON DB2INST1.CCBUABCD(BCMEVHY3X);
-
CREATE INDEX DB2INST1.BCMEVHY4
-
ON DB2INST1.CCBUABCD(BCMEVHY4X);
-
CREATE INDEX DB2INST1.BCMEVHY5
-
ON DB2INST1.CCBUABCD(BCMEVHY5X);
-
CREATE INDEX DB2INST1.BCMEVHY6
-
ON DB2INST1.CCBUABCD(BCMEVHY6X);
-
CREATE INDEX DB2INST1.BCMEVHY7
-
ON DB2INST1.CCBUABCD(BCMEVHY7X);
-
CREATE INDEX DB2INST1.BCMEVHY8
-
ON DB2INST1.CCBUABCD(BCMEVHY8X);
-
CREATE INDEX DB2INST1.BCMEVHY9
-
ON DB2INST1.CCBUABCD(BCMEVHY9X);
-
CREATE INDEX DB2INST1.BCMEVHYA
-
ON DB2INST1.CCBUABCD(BCMEVHYAX);
-
CREATE INDEX DB2INST1.BCMEVHYB
-
ON DB2INST1.CCBUABCD(BCMEVHYBX);
-
CREATE INDEX DB2INST1.BCMEVHYC
-
ON DB2INST1.CCBUABCD(BCMEVHYCX);
-
CREATE INDEX DB2INST1.BCMEVHYD
-
ON DB2INST1.CCBUABCD(BCMEVHYDX);
10、授权
db2 "grant select on CCBUABCD to user db2look"
db2 "grant select,insert,update,delete on CCBUABCD to user db2upt"
11、重组优化
db2 "reorg table DB2INST1.CCBUABCD use cbodtmp resetdictionary"
db2 "runstats on table DB2INST1.CCBUABCD with distribution and detailed indexes all"
13、备份到带库
dsmc archive /dbtmp/crj/20130925/ -subdir=yes
三、通知应用工程师执行如下操作:
1、恢复程序执行权限
2、程序绑定
阅读(5100) | 评论(0) | 转发(0) |