PHP Sleep Function 2 days time? - mysql

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

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 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.

Running multiple query concurrently

I've have a event which runs every morning at 1 AM.
The aim of the event is to do some calculation on the database and save it in a table.
I have a procedure, say spCalc which takes a unique mission_id as parameter. i have to calculate for 5 missions, so i have to execute the procedure like
call spCalc(1);
call spCalc(2);
..
call spCalc(5);
Can i run this stored procedures concurrently so i can insert into a table more faster.
Update:
The reason why i'm inserting data a table is:
I've to generate reports with more than 10 lakhs of records and to join more than 30 tables, as you know, it will take couple of hours to generate a report. We want to reduce this delay.
I'm doing some calculation like , i need to use max() function and to process all the data to find the highest. It would take lot of time when we process all the data.
So what we decided is to run the calculations every day morning and to save it, so it would be faster to retrieve.

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

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.

Changing values with MySQL in a table over time

I have a resource table for a game I'm trying to code, and each resource has a fixed income rate over time. But I can't find any description on how to increase the stored values of the MySQL Table over time automatically.
I'm using NetBeans to connect the program with the database, but I want the values to be updated on the server without the need to run the program. Otherwise I would just have had the time recorded and just add the time difference value.
Is there a way of doing this?
Table:
Player ID: 1
Gold: 100
Wood: 100
Increase rate: 50 per hour
One way of doing this is using Cron jobs and schedule some script to run.
Otherwise you can simply calculate the time elapsed from the beginning and (whithout updating your DB) calculate values based on the time when your program is running.
You can define a cron job on the server, that runs a query to update the values.
Yes, you can by adding a scheduled event like this. However, if you update the value in the database, the value/variable stored by the program will not be updated in real-time: you have to query the database for the updated value.