1.SHOW CREATE TABLE ooo_scrm_customer;
2.创建同ooo_scrm_customer表结构一样的新表:_ooo_scrm_customer_new
REATE TABLE _ooo_scrm_customer_new (
`id` int (11) NOT NULL AUTO_INCREMENT,
.....
.....
.....
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
3.创建增删改触发器(主要用于主表向中间表写数据时,业务方请求修改的数据要一起同步到中间表)
CREATE TRIGGER `ooo_scrm_customer_del` AFTER DELETE ON ooo_scrm_customer FOR EACH ROW DELETE IGNORE FROM _ooo_scrm_customer_new WHERE _ooo_scrm_customer_new.`id` <=> OLD.`id`
CREATE TRIGGER `ooo_scrm_customer_upd` AFTER UPDATE ON ooo_scrm_customer FOR EACH ROW REPLACE INTO _ooo_scrm_customer_new(`id`, `其他列`) VALUES (NEW.`id`, NEW.`其他列`)
CREATE TRIGGER `ooo_scrm_customer_ins` AFTER INSERT ON ooo_scrm_customer FOR EACH ROW REPLACE INTO _ooo_scrm_customer_new(`id`, `其他列`) VALUES (NEW.`id`, NEW.`其他列`)
4.将要保留的数据写入中间表
INSERT LOW_PRIORITY IGNORE INTO _ooo_scrm_customer_new (`id`, `其他列`) SELECT `id`, `其他列` FROM ooo_scrm_customer LOCK IN SHARE MODE # where 条件优化,按分批执行思想,稳步执行!
5.优化中间表
ANALYZE TABLE_ooo_scrm_customer_new
6.改名并删除操作记录
RENAME TABLE ooo_scrm_customer TO _ooo_scrm_customer_old, _ooo_scrm_customer_new TO ooo_scrm_customer
DROP TRIGGER IF EXISTS `mysqlcheck`.`ooo_scrm_customer_del`
DROP TRIGGER IF EXISTS `mysqlcheck`.`ooo_scrm_customer_upd`
DROP TRIGGER IF EXISTS `mysqlcheck`.`ooo_scrm_customer_ins`
|