有這麼個需求,有一張5000萬大表,需要保留最後3個月數據,也就是1000萬數據,而這張表使用很頻繁,生産環境也是7*24小時不停,如果用分段delete影響的時間太長,所以用了rename切換的方法。這裡先在測試數據庫做一下演練。
以測試環境BN_SEQUENCE表做測試,數據量大約是6千萬。
最近有點喜歡上畫圖,感覺形象點,可能有點醜,大家不要介意~
這裡的表定義、索引、主外鍵實際上用PLSQL就可以直接看到了,所以就不寫了,隻寫了觸發器的。
相關sql:
--查看表上觸發器定義 SELECT * FROM DBA_TRIGGERS WHERE TABLE_NAME='BN_SEQUENCE'; SELECT DBMS_METADATA.GET_DDL('TRIGGER','CHK_BIU_BN_SEQUENCE','GLOGOWNER') FROM DUAL; SELECT DBMS_METADATA.GET_DDL('TRIGGER','BN_SEQUENCE_PN','GLOGOWNER') FROM DUAL; -- Create table create table BN_SEQUENCE ( BN_RULE_GID VARCHAR2(101 CHAR) not null, BN_CONTEXT VARCHAR2(300 CHAR) not null, BN_SEQUENCE_ID VARCHAR2(50 CHAR) not null, CURVALUE VARCHAR2(50 CHAR), DOMAIN_NAME VARCHAR2(50 CHAR) not null, INSERT_USER VARCHAR2(128 CHAR) not null, INSERT_DATE DATE not null, UPDATE_USER VARCHAR2(128 CHAR), UPDATE_DATE DATE ) tablespace DATA pctfree 10 initrans 1 maxtrans 255 storage ( initial 1 next 1 minextents 1 maxextents unlimited pctincrease 0 ); .....
--這裡隻創建表定義,不加約束、索引、觸發器、外鍵 -- Create table create table BN_SEQUENCE_BAK ( BN_RULE_GID VARCHAR2(101 CHAR) not null, BN_CONTEXT VARCHAR2(300 CHAR) not null, BN_SEQUENCE_ID VARCHAR2(50 CHAR) not null, CURVALUE VARCHAR2(50 CHAR), DOMAIN_NAME VARCHAR2(50 CHAR) not null, INSERT_USER VARCHAR2(128 CHAR) not null, INSERT_DATE DATE not null, UPDATE_USER VARCHAR2(128 CHAR), UPDATE_DATE DATE ) tablespace DATA pctfree 10 initrans 1 maxtrans 255 storage ( initial 1 next 1 minextents 1 maxextents unlimited pctincrease 0 );
為了避免對線上環境的影響,建議分段insert,插入最近3個月的數據。
insert into BN_SEQUENCE_BAK select * from BN_SEQUENCE where update_date >=to_date('2019/08/19 00:00:00', 'yyyy/mm/dd hh24:mi:ss') and update_date <to_date('2019/09/19 14:00:00', 'yyyy/mm/dd hh24:mi:ss'); insert into BN_SEQUENCE_BAK select * from BN_SEQUENCE where update_date >=to_date('2019/07/19 00:00:00', 'yyyy/mm/dd hh24:mi:ss') and update_date <to_date('2019/08/19 00:00:00', 'yyyy/mm/dd hh24:mi:ss'); insert into BN_SEQUENCE_BAK select * from BN_SEQUENCE where update_date >=to_date('2019/06/19 00:00:00', 'yyyy/mm/dd hh24:mi:ss') and update_date <to_date('2019/07/19 00:00:00', 'yyyy/mm/dd hh24:mi:ss'); insert into BN_SEQUENCE_BAK select * from BN_SEQUENCE where update_date >=to_date('2018/06/19 00:00:00', 'yyyy/mm/dd hh24:mi:ss') and update_date <to_date('2019/06/19 00:00:00', 'yyyy/mm/dd hh24:mi:ss');
這裡實際上我在生産環境做切換也踏坑了,沒考慮到有物化視圖這種情況,所以導緻切換不了。
alter table BN_SEQUENCE rename to BN_SEQUENCE_ARCH; alter table BN_SEQUENCE_BAK rename to BN_SEQUENCE;
把前面插入數據後到切換表後的數據做一下補錄。
insert into BN_SEQUENCE select * from BN_SEQUENCE_ARCH where update_date >=to_date('2019/09/19 14:00:00', 'yyyy/mm/dd hh24:mi:ss')
記得需要重命名。
-- Add comments to the table comment on table BN_SEQUENCE is 'This table stores the current sequence value of the business number.'; -- Add comments to the columns comment on column BN_SEQUENCE.BN_RULE_GID is 'BN_RULE_GID contains the unique identifier for the Business Number (BN) rule.'; -- Create/Recreate primary, unique and foreign key constraints alter table BN_SEQUENCE add constraint PK_BN_SEQUENCE primary key (BN_RULE_GID, BN_CONTEXT, BN_SEQUENCE_ID) using index tablespace INDX pctfree 10 initrans 2 maxtrans 255 storage ( initial 1M next 1M minextents 1 maxextents unlimited pctincrease 0 ); alter table BN_SEQUENCE add constraint FK_BN_SEQRULE_GID foreign key (BN_RULE_GID) references BN_RULE (BN_RULE_GID); -- Grant/Revoke object privileges grant select, insert, update, delete on BN_SEQUENCE to APP_USER; grant select on BN_SEQUENCE to APP_USER_SELECT; grant select, insert, update, delete on BN_SEQUENCE to EXT_USER; ....
結果:數據全部遷移了,保留了去年6月19号到現在的數據,整個過程10分鐘
建議保留一段時間後再執行。
覺得有用的朋友多幫忙轉發哦!後面會分享更多devops和DBA方面的内容,感興趣的朋友可以關注下~
,
更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!