【MySQL】Event使用案例

本文介绍一个实际的使用案例
1、首先创建一个存储过程删除指定时间之前的数据。
delimiter //
CREATE PROCEDURE `proc_del_response_per_day`(in com_num int , in push_time datetime )
begin
declare curid bigint ;
DECLARE rowid bigint ;
declare no_more_departments int ;
declare curs cursor for
select response_per_day_id
from
response_per_day
WHERE
gmt_created < push_time ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_departments = 1;
SET no_more_departments=0;
set rowid = 1 ;
set autocommit = 0 ;
open curs ;
REPEAT
fetch curs into curid ;
delete from response_per_day where response_per_day_id = curid ;
set rowid = rowid + 1 ;
if rowid % com_num = 0
then
commit;
end if ;
UNTIL no_more_departments
END REPEAT;
commit ;
close curs ;
end;
//
delimiter ;

2、创建一个定时器每个一个月调度一次
create evnet e_call_proc_del_rpd
on schedule every 1 month
on completion preserve
do call(200,now());