Database periodic cleansing

Issue

I’m creating a public messaging service and I was wondering, in order to perform a cleanse of the oldest messages, is it okay to delete the oldest message every time a new one is submitted? Or is this approach inefficient for some reason? If so, could you specify why?

I considered create a Cron Job for this, but I’m not sure if it applies in this case.

Solution

You can schedule an event in MySQL:

DELIMITER $$

CREATE EVENT cleanup_messages ON SCHEDULE EVERY day ENABLE 
DO BEGIN
  DELETE FROM messages WHERE ......;
END $$

DELIMITER ;

http://dev.mysql.com/doc/refman/5.1/en/create-event.html

Answered By – Johan

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published