Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get MySQL to run queries on an interval?

Tags:

php

mysql

I'm creating a web application where every row of a table needs to be processed. I'm spawning one child PHP process per table row. I'm implementing a safety mechanism, so if a PHP process is interrupted processing a row, a new PHP process will spawned to process said row. To do this I'm going to create a new table where all PHP processes check in every 10 seconds or so. I need MySQL to delete all rows that haven't been checked into for 5 minutes or more, so my application will know to create a new PHP child to process that row.

I know it's possible to get MySQL to run queries on an interval, but I don't know how.

~Enter stackoverflow~


Edit: I was hoping to learn how to do this 100% MySQL. Is there no way to set MySQL to run a query every hour, or at a specific time each day or such?

like image 785
Hubro Avatar asked Dec 21 '25 23:12

Hubro


2 Answers

Crontab. You can run the query directly using the mysql client (mysql -uusername -ppassword dbname -e 'query here') or schedule a PHP script which runs the query.

DELETE FROM table WHERE checked_into < CURRENT_TIMESTAMP - INTERVAL 5 MINUTE
like image 102
Dan Grossman Avatar answered Dec 24 '25 12:12

Dan Grossman


MySQL Events are tasks that run according to a schedule. Therefore, we sometimes refer to them as scheduled events. ... Conceptually, this is similar to the idea of the Unix crontab (also known as a “cron job”) or the Windows Task Scheduler.

http://dev.mysql.com/doc/refman/5.1/en/events-overview.html

And here is the lovely syntax: http://dev.mysql.com/doc/refman/5.1/en/create-event.html

like image 39
Farray Avatar answered Dec 24 '25 11:12

Farray



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!