一、背景 由於項目的業務是不斷往前跑的,所以難免資料庫的表的量會越來越龐大,不斷的擠占硬碟空間。即使再大的空間也支撐不起業務的增長,所以定期刪除不必要的數據是很有必要的。在我們項目中由於不清理數據,一個表占的空間竟然達到了4G之多。想想有多可怕... 這裡介紹的是用MySQL 建立一個定時器Even ...
一、背景
由於項目的業務是不斷往前跑的,所以難免資料庫的表的量會越來越龐大,不斷的擠占硬碟空間。即使再大的空間也支撐不起業務的增長,所以定期刪除不必要的數據是很有必要的。在我們項目中由於不清理數據,一個表占的空間竟然達到了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;