mysql定期删除过期数据
1.首先先查看mysql是否开启定时任务开关
SHOW VARIABLES LIKE 'event_scheduler';
2.Value为ON则已打开,OFF则关闭
如果是OFF,就先打开:
SET GLOBAL event_scheduler = ON;
3.创建想要的定时器
DELIMITER $$
DROP EVENT IF EXISTS deleteData;
CREATE EVENT deleteData
ON SCHEDULE EVERY 300 SECOND //这里300即为每300秒删除一次
ON COMPLETION PRESERVE
DO BEGIN
delete from algm_status where TO_DAYS(now())-TO_DAYS(CREATED_TIME)>2; //距现在超过两天即视为过期,CREATED_TIME是字段名
END$$