MySQL定时任务event,储存过程(定时删除指定时间前90天指定表的数据)
- <span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">MySQL定时任务event</span>
- CREATE TABLE `tbl_base_count` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `CAPTCHA` varchar(12) COLLATE utf8_bin NOT NULL,
- `PHONE` varchar(12) COLLATE utf8_bin NOT NULL,
- `SENDTIME` varchar(32) COLLATE utf8_bin NOT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
2.创建事件e_del_tbl_base_count:
- CREATE EVENT `e_del_tbl_base_count`
- ON SCHEDULE EVERY 1 DAY STARTS ‘2013-06-23 17:33:43‘
- ON COMPLETION NOT PRESERVE ENABLE DO CALL p_del_count (90);
- p_del_count
- DELIMITER $$
- --
- -- 存储过程
- --
- CREATE PROCEDURE `p_del_count`(IN `date_inter` INT)
- BEGIN
- DELETE FROM LOG WHERE (TO_DAYS(NOW()) - TO_DAYS(FROM_UNIXTIME(<span style="font-family: Consolas, ‘Courier New‘, Courier, mono, serif; line-height: 18px;">SENDTIME</span>)))>=date_inter;
- END$$
- DELIMITER ;
- SHOW VARIABLES LIKE ‘event_scheduler‘;
- SELECT @@event_scheduler;
- SHOW PROCESSLIST;
- SET GLOBAL event_scheduler = 1;
- SET @@global.event_scheduler = 1;
- SET GLOBAL event_scheduler = ON;
- SET @@global.event_scheduler = ON;
6.(三)事件开启与关闭:
- 开启某事件:ALTER EVENT e_del_logs ON COMPLETION PRESERVE ENABLE;
- 关闭某事件:ALTER EVENT e_del_logs ON COMPLETION PRESERVE DISABLE;
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。