问题
Is it possible to make a stored procedure that run every night 11 pm , check in table if any record is modified for last six month, If some record is modified for last six month I have to delete it from table. This has to run automatically without use of any external language.
回答1:
CREATE EVENT IF NOT EXISTS `my_old_data_cleaner_event`
ON SCHEDULE EVERY 23 DAY_HOUR
COMMENT 'Clean up Service Start at 11:00PM daily!'
DO DELETE FROM my_table WHERE created_date < (NOW() - INTERVAL 1 MONTH);
OR for Stored Procedure.
CREATE EVENT IF NOT EXISTS `my_old_data_cleaner_event`
ON SCHEDULE EVERY 23 DAY_HOUR
DO CALL my_sp_cleanup_old_data();
回答2:
you can achieve with mysql event scheduler--
http://dev.mysql.com/doc/refman/5.1/en/events.html
detail blog: http://goo.gl/6Hzjvg
回答3:
Create an event like below
CREATE EVENT e_daily
ON SCHEDULE
EVERY 1 DAY
DO
BEGIN
DELETE FROM tableA WHERE DATE(`yourtimestamp`) <(CURDATE() - INTERVAL 6 MONTHS);
END
回答4:
You can also write a script that processes your data in Python, Perl, PHP, etc.
After that, simply setup cron entry using crontab -e
and add following line:
0 23 * * * /path/to/my/script.pl 2>&1 >/dev/null
If you don't specify 2>&1 >/dev/null
, you will get email with execution results.
来源:https://stackoverflow.com/questions/13872598/autorunning-query-in-mysql