MySQL数据库 Event 定时执行任务.
2018-06-18 00:37:17来源:未知 阅读 ()
一、背景
由于项目的业务是不断往前跑的,所以难免数据库的表的量会越来越庞大,不断的挤占硬盘空间。即使再大的空间也支撑不起业务的增长,所以定期删除不必要的数据是很有必要的。在我们项目中由于不清理数据,一个表占的空间竟然达到了4G之多。想想有多可怕...
这里介绍的是用MySQL 建立一个定时器Event,定期清除掉之前的不必要事件。
二、内容
#1、建立存储过程供事件调用 delimiter// drop procedure if exists middle_proce// create procedure middle_proce() begin DELETE FROM jg_bj_comit_log WHERE comit_time < SUBDATE(NOW(),INTERVAL 2 MONTH); optimize table jg_bj_comit_log; DELETE FROM jg_bj_order_create WHERE created_on < SUBDATE(NOW(),INTERVAL 3 MONTH); optimize table jg_bj_order_create; DELETE FROM jg_bj_order_match WHERE created_on < SUBDATE(NOW(),INTERVAL 3 MONTH); optimize table jg_bj_order_match; DELETE FROM jg_bj_order_cancel WHERE created_on < SUBDATE(NOW(),INTERVAL 3 MONTH); optimize table jg_bj_order_cancel; DELETE FROM jg_bj_operate_arrive WHERE created_on < SUBDATE(NOW(),INTERVAL 3 MONTH); optimize table jg_bj_operate_arrive; DELETE FROM jg_bj_operate_depart WHERE created_on < SUBDATE(NOW(),INTERVAL 3 MONTH); optimize table jg_bj_operate_depart; DELETE FROM jg_bj_operate_login WHERE created_on < SUBDATE(NOW(),INTERVAL 3 MONTH); optimize table jg_bj_operate_login; DELETE FROM jg_bj_operate_logout WHERE created_on < SUBDATE(NOW(),INTERVAL 3 MONTH); optimize table jg_bj_operate_logout; DELETE FROM jg_bj_operate_pay WHERE created_on < SUBDATE(NOW(),INTERVAL 3 MONTH); optimize table jg_bj_operate_pay; DELETE FROM jg_bj_position_driver WHERE created_on < SUBDATE(NOW(),INTERVAL 3 MONTH); optimize table jg_bj_position_driver; DELETE FROM jg_bj_position_vehicle WHERE created_on < SUBDATE(NOW(),INTERVAL 3 MONTH); optimize table jg_bj_position_vehicle; DELETE FROM jg_bj_rated_passenger WHERE created_on < SUBDATE(NOW(),INTERVAL 3 MONTH); optimize table jg_bj_rated_passenger; end// delimiter; #2、开启event(要使定时起作用,MySQL的常量GlOBAL event_schduleer 必须为on 或者1) show variables like 'event_scheduler' set global event_scheduler='on' #3、创建Evnet事件 drop event if exists middle_event; create event middle_event on schedule every 1 DAY STARTS '2017-12-05 00:00:01' on completion preserve ENABLE do call middle_proce(); #4、开启Event 事件 alter event middle_event on completion preserve enable; #5、关闭Event 事件 alter event middle_event on completion preserve disable;
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- MySQL replace函数怎么替换字符串语句 2020-03-09
- PHP访问MySQL查询超时怎么办 2020-03-09
- mysql登录时闪退 2020-02-27
- MySQL出现1067错误号 2020-02-27
- mysql7.x如何单独安装mysql 2020-02-27
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash