Mysql self deleting records
Is there a way to automatically delete old records. Each record has a timestamp. I want the records with a timestamp older than 10minutes ago to be deleted. Is there a way to do this within mysql or i need to set an external program t开发者_如何学Co check the timestamps and decide whether or not it should be deleted?
How about doing that in a trigger? Whenever you change some table's data (insert, update, delete), you'd delete the old data of the table.
What, no one changes the data of the table?? Then why would you bother deleting old stuff if no one messes around with the data? :)
On MySQL 5.1+ you can create an event to delete from the table where the timestamp is less then now - 10 minutes. Have the event run every minute, or as often as you wish. See the manual for more. Something like
CREATE EVENT 'prune_table'
ON SCHEDULE EVERY 5 MINUTE
DO DELETE FROM myTable WHERE timestamp < NOW() - 600;
As far as I know there's no way to do this within MySQL, you could write a short bash script, perhaps even a one liner and have that run on a cron. Hacky, but it'd do the job.
精彩评论