How to check event execution duration in mysql? - mysql

I am using mysql 5.7 and I scheduled some simple BI calculation using events.
The events are scheduled every 1 hour and I need to know duration of each execution to evaluate if the event can be scheduled every 30 mins.
I can use
SELECT * FROM INFORMATION_SCHEMA.events;
to find events' status, last run time, last altered time, etc. But where to find the execution log of events?

Related

MySQL Event Scheduler run daily in a specific time frame

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;

MySQL SELECT record where equals NOW() not enough time to process

I'm sending alerts out when a scheduled job has ended. I'm selecting records where end date and time equal NOW(). The script runs every minute via cronjob.
WHERE TIMESTAMP(end_date, end_time) = NOW()
The alerts are not being sent out because the cronjob is running every minute and I'm selecting records based on NOW(), which is to the second. So it's almost impossible that the script is run at the same time a job ends.
Is there anyway I can select records which have an end time within the current minute which the cronjob runs? I want to make sure I'm catching these jobs as soon as they end.

recurrent job runs only once

I have a SQL job schedule to run daily. There are 8 jobs running with the same schedule. 6 jobs run properly. The remaining two run only once when i start the jobs manually, subsequent recurring runs never happen. There is no information about these two in job history.
It looks like the scheduler does not invoke the mentioned two jobs at all.
Workaround: set the schedule to run once in 24 hours, added start time and end times with 30 minutes of interval.

MySql - Missed event schedule

I am trying to use mysql event schedule in my application, I have not use it before so i have some confusions.
I want to know if my computer is off on the schedule date, then schedule will continue on next day, after starting my computer?
Like:
my schduled is for beginning at every month (no predefined time set)
if in the above date my computer/Server is off,
will mysql continue scheduled event in next day after turning on my computer/server?
If no, then please suggest a solution.
Hmmmm, have you looked at something like this?
MySQL: Using the Event Scheduler
... or:
How to create MySQL Events
... or even: [MySQL :: MySQL 5.1 Reference Manual: 19.4.1. Event Scheduler Overview](19.4.1. Event Scheduler Overview)?
Also please keep in mind that SQL DBMS servers are written with the rather strong presumption that they will be kept up and operating 24 hours per day with only brief periods of downtime for maintenance or repairs. There is generally very little consideration for operation on machines which are shutdown at night and while not in use.
If you simply store a table of dates and events then your can simply query that table for events which have passed or are upcoming within any range you like ... and you can run the program(s) containing those queries (and performing any appropriate activities based on the results) whenever you start you computer and periodically while it's up and running.
These links refer to a feature of MySQL which is designed to have the server internally execute certain commands (MySQL internal commands, such as re-indexing, creating/updating views, cleaning tables of data which "expires" and so on. I don't know if a MySQL server would attempt to execute all events which have passed during downtime, though it should only be a little bit of work to follow the tutorial, schedule some event for some time (say 15 minutes after the time you expect to hit [Enter]) ... then shutdown your computer (or even just the MySQL server) and go off to lunch. Then come back, start it up and see what happens.
The scheduled event could be something absurdly simple, like inserting the "current" time into some table you set up.

Way to automate deletion of expired records in MySQL?

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.