I wrote a query in MySQL which I want to run daily on our wordpress server. Using Cronjobs gave me a lot of collate errors, so I took the easier rout with the Event Scheduler which is easier to implement via phpMyAdmin.
Now I would like to run the query only at specific times: Once per hour, between 8 am and 5 pm.
Is this possible using Event Scheduler? I know of:
STARTS = ...
ENDS = ...
But as far as I know, that only sets global values (start and end once). Is there any possibility to set a recurring daily end? Or do I have to use cron for that?
Issue
The problem from what I see is that you want to do two things:
Run something once per hour (interval)
But not continue doing this action outside of a specific daily timeframe.
The first part is very easily to take care of using the example from:
MySQL Event Scheduler on a specific time everyday
The second part is the one that creates the complexity.
Solutions
You have two available solutions that I can think of:
Schedule an event with interval 1 hour, and add a check in your SQL statement / function / procedure to check that that time of day is within the specific time interval you want.
Schedule multiple events (9-10) for the specific times e.g. (8:59am, 9:59am...4:59am)
Personally I would prefer solution one since it is much easier to change a value in your script than have to reschedule / manage 9-10 events.
Quick Example
delimiter |
CREATE EVENT hours
ON SCHEDULE
EVERY 1 HOUR
STARTS (TIMESTAMP(CURRENT_DATE))
DO
proc_label:BEGIN
IF curtime() >= "08:00" OR curtime() <= "17:00" THEN
LEAVE proc_label;
END IF;
select 1+1;
END |
delimiter ;
Just a little sugestion, to change from OR to AND.
IF curtime() >= "08:00" AND curtime() <= "17:00" THEN
LEAVE proc_label;
Related
I Have an OTP table and I want to delete data that is older than 5 minutes automatically
Then how could I make trigger or procedure for that?
You must use according Event Scheduler procedure.
CREATE EVENT remove_old_rows
ON SCHEDULE
EVERY 10 SECOND
COMMENT 'Delete the rows that are older than 5 minutes from OTP table.'
DO
DELETE
FROM OTP_database.OTP_table
WHERE created_at < CURRENT_TIMESTAMP - INTERVAL 5 MINUTE;
Do not forget to enable Event Scheduler.
Don't do it! Just create view to get the most recent data:
create view v_otp as
select otp.*
from otp
where otp.created_at >= now() - interval 5 minute;
Anyone who uses the view only sees the most recent data.
Then you can leisurely delete old data during a period when the database is not busy.
An added benefit is that this is always accurate. If an event or job gets delayed, then your users might see old data. Further, this does not involve complicated locking and transaction semantics when the server is busy.
I've built several stored procedures in MySQL. I would like to run them in a specific order to ensure that tables are updated properly and efficiently. What would be the best way to call the procedures to run in a specific order? I would like to have them run once every 30 minutes or so.
Thanks
After some research, I found that a recurring event for each procedure is a good way to go. Creating each event about 10 seconds apart ensures they run in sequence every 30 minutes.
CREATE EVENT `[Event_Name]`
ON SCHEDULE EVERY 30 MINUTE
DO
CALL `[Stored_Procedure_Name]`();
I am using a host gator shared hosting plan, and need to execute a simple command every minute:
UPDATE table_info SET expired = 1 WHERE TIMESTAMP(dateTime) <= NOW()
My problem is that I cant turn on the event scheduler because I don't have permissions for it. Can anyone think of a simple way to do this without using events?
You can put your update statement into simple PHP script, and run it via cron.
Using cron or any other OS based task scheduler is stricktly speaking an answer to this question.
However, it is not really a best practice to run any event every minute. Particularly not, if the operation done could be executed on the fly, like here. The expired flag's value depends on a simple calculation:
dateTime <= NOW()
Instead of running this calculation every minute on the entire table_info, you could
place the sql command into a regularly executed script
just embedd the logic into any reporting or other operation which depend on the expired field and get rid of the flag completely.
I have a stored procedure I'd like to run forever, but sleep for one second in a loop. When it wakes up it would poll a table to see if it should do some work. Work only needs to be done every minute, so there is no worry about the poll table getting hit with updates from two writers at the same time.
What is the best way to SLEEP() for an interval in a stored procedure? It would be nice, actually, if it could sleep for 200 milliseconds, but one second would work too.
I've encountered the same problem. After googling a lot, I found out that we can use
SELECT SLEEP(<seconds>);
to delay our procedures for this many seconds. In your case, using
SELECT SLEEP(0.2);
would be just fine.
You can use:
DO SLEEP(0.2);
Reference: http://dev.mysql.com/doc/refman/5.7/en/do.html
or
SELECT SLEEP(0.2);
Reference: http://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_sleep
MySQL has an event scheduler baked in. https://dev.mysql.com/doc/refman/5.6/en/events-overview.html
Sample:
CREATE EVENT performance_schema_snapshots.fill_events_statements_summary_by_digest_history1
ON SCHEDULE -- every day at 6 am
EVERY 1 DAY
STARTS TIMESTAMP(CURRENT_DATE) + INTERVAL 1 DAY + INTERVAL 14 HOUR
DO
-- take snapshot
CALL performance_schema_snapshots.events_statements_summary_by_digest_snapshot_reset ();
You dont specify which database you are using, but generally the way to achive what you want is not to have an infinetly running sproc but to have some external component - like a scheduler, or SQL Server Agent in MSSQL - execute the sproc every so often.
I've googled around and searched the MYSQL docs ad nauseam and couldn't find a succinct way of automating deletion of records that exceeded a given timeframe. I've been able to get a query in 5.1 to cast a value of TIMESTAMP to DATETIME within a DIFF function with the current time to see if it meets the criteria of expiration. I've read that 5.1 now has the capability of running scheduled tasks but not much in the way of configuring it. I'm not using triggers for this.
In the MySQL docs for 5.1, it refers to creating an event:
'CREATE
[DEFINER = { user | CURRENT_USER }]
EVENT
[IF NOT EXISTS]
event_name
ON SCHEDULE schedule
[ON COMPLETION [NOT] PRESERVE]
[ENABLE | DISABLE | DISABLE ON SLAVE]
[COMMENT 'comment']
DO sql_statement;
schedule:
AT timestamp [+ INTERVAL interval] ...
| EVERY interval
[STARTS timestamp [+ INTERVAL interval] ...]
[ENDS timestamp [+ INTERVAL interval] ...]
interval:
I currently use Toad (which has been a Godsend). My query affectively removes any records that are more than 30 minutes old. I just need to find how this event gets invoked...
Thanks!
You are talking about using the MySQL Scheduler. Once you create that event, MySQL will call it automatically at whatever interval you configure it with. If you are having trouble getting it set up, post the query and error your are getting.
Write a query and have it ran on a job every so often. Say, check for the expired rows every 30 minutes or so.
If it doesn't have to be exact, and you're just housekeeping, you can tie the process to another one. If you can afford the time.
If you have an old invoice file, purge it when month-end is run (possibly a lot of records, but it's a batch process anyway). Purge old inventory items when you add new ones (less frequent, but fewer records possibly). Keeping an access log table? Purge it when the most recent record in it falls on a different day than today. (for low traffic logfiles) And so on.