【微服务|Go 微服务十七 存储过程 + 事件 实现数据备份】点击链接查看我的个人博客,文章更全更详细
遇到一个需求。需要每天凌晨三点实现对指定的几张表执行备份(备份前一天的数据)。并且写到备份库里,并对现有库中删除掉。每天的单子量非常大,如果再加上备份读写 mysql 会比较慢
刚开始通过go协程开四个协程实现备份,一个小时备份了 不到50万太慢了。所以改用存储过程+事件的方式实现。经测试
530万的数据量 备份需要大概13分钟。
创建demo 数据库
创建 o_a_order表
创建 o_b_order表
创建 o_c_order表
创建 o_match_order表
备份的表
创建 o_a_order_backup表
创建 o_b_order_backup表
创建 o_c_order_backup表
创建 o_match_order_backup表
- 查看mysql 事件调度器是否打开
show variables like '%event_scheduler%';
- 开启event_scheduler sql指令
SET GLOBAL event_scheduler = ON;
SET @@global.event_scheduler = ON;
SET GLOBAL event_scheduler = 1;
SET @@global.event_scheduler = 1;
- o_a_order
-- 存储过程 o_a_order
DELIMITER |DROP PROCEDURE IF EXISTS a_backup |CREATE PROCEDURE a_backup()BEGININSERT INTO o_a_order_backup SELECT * FROM o_a_order WHERE created_at < CAST(CAST(SYSDATE()AS DATE)AS DATETIME);
DELETE FROM o_a_order WHERE created_at < CAST(CAST(SYSDATE()AS DATE)AS DATETIME);
END|-- 定时器 omp_a_order
SET GLOBAL event_scheduler = 1;
CREATE EVENT IF NOT EXISTS a_backupON SCHEDULE EVERY 1 DAYON COMPLETION PRESERVEDO CALL a_backup();
-- 启动定时器 omp_a_order
ALTER EVENT a_backup ON
COMPLETION PRESERVE ENABLE;
- o_b_order
-- 存储过程 o_b_order
DELIMITER |DROP PROCEDURE IF EXISTS b_backup |CREATE PROCEDURE b_backup()BEGININSERT INTO o_b_order_backup SELECT * FROM o_b_order WHERE created_at < CAST(CAST(SYSDATE()AS DATE)AS DATETIME);
DELETE FROM o_b_order WHERE created_at < CAST(CAST(SYSDATE()AS DATE)AS DATETIME);
END|---- 定时器 omp_b_order
SET GLOBAL event_scheduler = 1;
CREATE EVENT IF NOT EXISTS b_backupON SCHEDULE EVERY 1 DAYON COMPLETION PRESERVEDO CALL b_backup();
-- 启动定时器 omp_b_order
ALTER EVENT b_backup ON
COMPLETION PRESERVE ENABLE;
- o_c_order
DELIMITER |DROP PROCEDURE IF EXISTS c_backup |CREATE PROCEDURE c_backup()BEGININSERT INTO o_c_order_backup SELECT * FROM o_c_order WHERE created_at < CAST(CAST(SYSDATE()AS DATE)AS DATETIME);
DELETE FROM o_c_order WHERE created_at < CAST(CAST(SYSDATE()AS DATE)AS DATETIME);
END|---- 定时器 o_c_order
SET GLOBAL event_scheduler = 1;
CREATE EVENT IF NOT EXISTS c_backupON SCHEDULE EVERY 1 DAYON COMPLETION PRESERVEDO CALL c_backup();
-- 启动定时器 omp_c_order
ALTER EVENT c_backup ON
COMPLETION PRESERVE ENABLE;
- o_match_order
-- 存储过程 o_match_order
DELIMITER |DROP PROCEDURE IF EXISTS m_order_backup |CREATE PROCEDURE m_order_backup()BEGININSERT INTO o_match_order_backup SELECT * FROM o_match_order WHERE created_at < CAST(CAST(SYSDATE()AS DATE)AS DATETIME);
DELETE FROM o_match_order WHERE created_at < CAST(CAST(SYSDATE()AS DATE)AS DATETIME);
END|---- 定时器 o_match_order
SET GLOBAL event_scheduler = 1;
CREATE EVENT IF NOT EXISTS match_order_backupON SCHEDULE EVERY 1 DAYON COMPLETION PRESERVEDO CALL m_order_backup();
-- 启动定时器 o_match_order
ALTER EVENT m_order_backup ON
COMPLETION PRESERVE ENABLE;
show events;
SHOW PROCEDURE STATUS LIKE 'a_backup';
SHOW PROCEDURE STATUS LIKE 'b_backup';
SHOW PROCEDURE STATUS LIKE 'c_backup';
SHOW PROCEDURE STATUS LIKE 'match_backup';
drop event a_backup;
drop event b_backup;
drop event c_backup;
drop event match_order_backup;
推荐阅读
- 数据库学习|【数据库学习】——【汇总】MySQL数据库下载与安装(Msi文件安装和免安装版本)、Navicat下载与安装
- #|08-MySQL事务
- java|MYSQL优化自学笔记
- 在WordPress页面中从MySQL插入和查询数据
- SQL数据库|SQL数据库实操 第五波 完整性约束和触发器
- 数据库|Oracle数据库
- mysql|阿里一面(SQL 优化有哪些技巧)
- MySql|MySQL之四种SQL性能分析工具
- php|PHP与MySQL数据库——连接数据、插入数据、删除数据、返回结果集