Is there a way for SLEEP() in a stored procedure? - mysql

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.

Related

MySQL - calling several stored procedures in a specific order

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]`();

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;

PHP Sleep Function 2 days time?

I'm trying to find a way to execute a function (send some emails), 2 days after a new table is inserted into the database. I would like to do this without cron if possible, so I was wondering if is too wrong to use the sleep function, with 2 days time? Or any other suggestion..
You can use SCHEDULE to set schedule for database query.
Maybe this question is already solved here

mySQL event alternative

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.

SQL Server Job with precise timing

I have a DB with game data (map, players, etc...) and I have a game core mechanics written in T-SQL stored procedure.
I need process game loop (via the stored procedure) every "X" seconds.
I tried used the SQL Job, but when I set the interval to seconds, the SQL server stops responding. If I set the interval greater than one minute, all was ok.
I need game loop precise in time, e.g. the game loop will run only once and will be executed every "X" precisely (tolerance should be less than one second).
Can I do it with SQL Server capabilities? Or should I create a windows service which will repeatly execute game loop procedure? Or should I go another way?
Thanks!
EDIT:
The game loop stored procedure takes less than the interval.
I would use a windows service for this. Have a loop with a thread sleep in it to get it to wait every x seconds.
The problem with timer jobs of type used in SQL server, is that there is a timer service checking if there is a job that need to be run. This timer job may check for example every 2 minutes, so precision down to a second is not possible.
To run smth with interval of 1 sec you can use code:
CREATE PROCEDURE SP_Execute_job
AS
WHILE 1=1
BEGIN
WAITFOR DELAY '00:00:01'
EXECUTE 'somewhat job'
END
END
This sp should be executed after each SQL Server start:
exec sp_procoption N'SP_Execute_job', 'startup', 'on'