首页 > 数据库 > MySQL > MySQL 对大表做归档
方案:为尽量降低对业务影响,决定采取下列方案。
1、在主库建立 2016、2017、2018、2019的4个历史表结构。
2、在从库建立test库,并建立 2016、2017、2018、2019的4个历史表结构,在从库的主表上用insert into语句根据时间字段把数据插入test库的2016、2017、2018、2019的历史表里面。分拆为2016、2017、2018、2019。
3、用Navicat把 2016、2017、2018、2019导出为SQL文件,并生成主表的DELETE语句的TXT文件。
4、用Python脚本把 SQL文件和 TXT文件进行处理,分批导入到 2016、2017、2018、2019的4个历史表,并删除主表的历史数据。
5、对主表进行收缩。
完成归档。
1、在主库建立历史表的表结构。
CREATE TABLE `upload_order_header_2016` (
`id` bigint(22) NOT NULL AUTO_INCREMENT COMMENT '自增id',
`company` varchar(25) DEFAULT NULL COMMENT '货主',
PRIMARY KEY (`id`) 郑州胎记医院哪家好 http://www.zykdyjyy.com/
) ENGINE=InnoDB DEFAULT CHARSET=utf8
2、从库建立test库,同样建立 历史表的表结构。
在从库上用insert into语句把2016年的历史数据插入test库的2016年的历史表。
insert into test.upload_order_header_2016 select * from log_db.upload_order_header
where add_time < unix_timestamp('2017-01-01 00:00:00');
insert into test.upload_order_header_2017 select * from log_db.upload_order_header
where add_time >= unix_timestamp('2017-01-01 00:00:00') and
add_time < unix_timestamp('2018-01-01 00:00:00');
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69979119/viewspace-2719477/ ,如需转载,请注明出处,否则将追究法律责任。