Pregunta

I have to update my MySQL database every hour, and I was wondering what the advantages/disadvantages of using a cronjob VS a MySQL event? For example, which is faster? Which is safer? Thanks!

¿Fue útil?

Solución

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.

Otros consejos

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

i am going with mysql event schedular

because we don't have to code one extra file for it, our purpose can be fulfilled by just writing one query.

if only database related operation required then mysql event schedular is good choice.

Mysql event scheduler is on the advantage for shared host environment. In terms of timing, it can also be scheduled down to the second. Unlike cron, it's by the minute - but you can work around this limitation however.

I'm working with EVENTs myself now and pondered the same :)

In addition to the above answers:

  • Use EVENTs if the task is purely data-centric, or complements functionality that is already build into the database. You may already have triggers that clean up data, log certain events, aggregate certain data, etc. If the scheduled task you want to perform is part of the existing design, it's more cohesive to trigger the task from an EVENT. EVENTs are temporal triggers after all.

  • If you run a script from a shell, you need a username/password stored in the shell script or in a defaults file. For executing EVENTs, you don't need a username/password after the EVENT has been created.

  • IMCO you should write the logic in a stored procedure; then invoke the procedure from an EVENT or from a shell script, whatever suits you best. You might even build some place in your UI that enables users to invoke the procedure manually.

This have been a long time question but I guess one advantage of using mysql events is its directly embedded within the dbms so it does not need to create/maintain a connection if for example you are using php as your main language you could also consider this cause with that you eliminate the necessity for validation upon creating the connection to the db before being able to run a statement and also you can create an event using a role that could do maintenance scripts and assign a user role to the application that does not that will give you security that you know that users only have this kind of role could access so deciding whether to use events or cron job depends on the system your working on. But another thing to watch out is multiple trigger of the same event especially if the interval is just a difference is just a matter of seconds like every 5-10secs etc.

I just spent the past few hours reading the Event Scheduler documentation and I've just used it to create an event that's supposed to recur daily.

I'm actually very pleased with it and I think it's a better choice compared to Cronjob, at least for the reason that you don't need to use an external script.

Performance-wise, my bet is that it's a much better option.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top