日志正文
|
||
使用mysql存储过程和定时任务1.存储过程 # 存储过程:将6个月前的数据放入历史表中 DELIMITER // create procedure p_move_data_to_history() begin -- 数据剪切截止日期 -- 5个月前的一号 set @historyMonthLine = date_add( date_format(now(), '%Y-%m-01'), interval -5 month); -- 6个月前的最后一号 set @historyDateLine = date_add( @historyMonthLine, interval -1 day); if exists (select 1 from day_report where attendance_date <= @historyDateLine limit 1) then start transaction; insert clock_record_history select * from clock_record where date < @historyMonthLine; delete from clock_record where date < @historyMonthLine; insert day_clock_history select * from day_clock where date <= @historyDateLine; delete from day_clock where date <= @historyDateLine; insert employee_day_report_history select * from employee_day_report where date <= @historyDateLine; delete from employee_day_report where date <= @historyDateLine; commit; end if; end // DELIMITER ; # 调用存储过程 call p_move_data_to_history(); 2.定时任务 # 定时任务;每月1号执行一次 delimiter $$ alter event job_move_data_to_history # 每分钟执行一次 #on schedule every 1 minute starts timestamp '2018-07-11 10:55:00' # 每天上午10点执行一次 #on schedule every 1 DAY starts timestamp '2018-07-11 10:00:00' # 每月1号0点10分执行一次 on schedule every 1 month starts '2018-07-01 00:10:00' -- ON COMPLETION NOT PRESERVE ENABLE do begin #查看当前是否已开启事件调度器 # show variables like 'event_scheduler'; #要想保证能够执行event事件,就必须保证定时器是开启状态,默认为关闭状态 # set global event_scheduler =1; #或者set GLOBAL event_scheduler = ON; # 注意:真实的开发环境中,会遇到mysql服务重启或者断电的情况,此时则会出现事件调度器被关闭的情况,所有事件都不在起作用,要想解决这个办法,则需要在mysql.ini文件中加入event_scheduler = ON; 的语句 # 停止 # ALTER EVENT SetEVToInvalidStatus_AtNight_0100 DISABLE; # 开启 # alter event SetEVToInvalidStatus_AtNight_0100 enable; # 查看状态 # select * from mysql.event # 查看event内容定义 # SELECT * FROM information_schema.events; call p_move_data_to_history(); end $$ delimiter ;
阅读(?)评论(0)
上一篇: 备份Linux下的redis数据
下一篇:监控mysql执行的SQL
|
||
评论 想第一时间抢沙发么?