Cronjob or MySQL event?

I would always go a cron job, because:

  • That's where sysadmins will expect it to be (this point is not to be underestimated)
  • crontab is bullet-proof, time-tested, extremely widely used and understood
  • You can freely direct/analyse error/success messages where you want
  • Some database tasks require/prefer mysql to be off-line (eg full backup), so you've got to use cron for those - it's a bad idea to have some tasks done with cron and some done with mysql; you'll be unsure where to look
  • You can chain up other events that should follow if you've got a shell script

And finally, just because you can do something, doesn't mean it's a good idea. Mysql is good at data stuff. Don't use it for "shell" stuff.


MySQL Event Scheduler – A good replacement for cron.

We all know about cron, an easy way to schedule certain processes, like truncating your log tables in your MySQL database every week.

With MySQL 5.1 the guys at MySQL introduced a new cool feature: The MySQL Event Scheduler !

With the Event Scheduler you can schedule tasks that you want to perform on your database. This is great for web developers who can’t create cron jobs on their webspace, because their host won’t let them! It really is a great replacement for cron!

A few examples:

you want to truncate your application log table every week, this is how your event schedule should look like:

CREATE EVENT PurgeLogTable
ON SCHEDULE EVERY 1 WEEK
DO
BEGIN
DELETE FROM `logs` WHERE `LogTime` <= DATE_SUB(CURRENT_TIMESTAMP,INTERVAL 1 WEEK);
INSERT INTO `audit` (`AuditDate`, `Message`) VALUES(NOW(), "Log table purged succesfully!");
END

Mysql introduce Event scheduler which we can use alternative to Cronjob. There are many advantages over cronjob like:

1)It is directly written on Mysql Server.

2) This is platform independent. Your application might be written in any language it does not matters. You just need to know mysql.

3) We can use them whenever there is a database update or cleanup required at regulare interval.

4) No need to compile queries every time hence performace increased.

5) Error can be log in log files. Syntax:

DELIMITER //
CREATE EVENT eventName
ON SCHEDULE EVERY 1 WEEK
STARTS 'Some Date to start'
ENDS 'End date If any' 

DO
BEGIN
   // Your query will be here
END//
DELIMITER ;

For more information you can visit official site:http://dev.mysql.com/doc/refman/5.1/en/create-event.html

detail blog: http://goo.gl/6Hzjvg