MySQL - calling several stored procedures in a specific order - mysql

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

Related

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

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.

Multiple queries VS Stored Procedure

I have an application
which does around 20000 DATA-OPERATIONS per/hour
DATA-OPERATION has overall 30 parameters(for all 10 queries). Some are text, some are numeric. Some Text params are as long as 10000 chars.
Every DATA-OPERATION does following:
A single DATA-OPERATION, inserts / updates multiple tables(around 10) in database.
For every DATA-OPERATION, I take one connection,
Then I use new prepared-statement for each query in the DATA-OPERATION.
Prepared-statement is closed every time a query is executed.
Connection is reused for all 10 prepared-statements.
Connection is closed when DATA-OPERATION is completed.
Now to perform this DATA-OPERATION,
10 queries, 10 prepared-statement(create, execute, close), 1o n/w calls.
1 connection (Open,Close).
I personally think that, if I create a Stored Procedure from above 10 queries, it will be better choice.
In case of SP, DATA-OPERATION will have:
1 connection, 1 callable statement, 1 n/w hit.
I suggested this, but I am told that
This might be more time consuming than SQL-queries.
It will put additional load on DB server.
I still think SP is a better choice. Please let me know your inputs.
Benchmarking is an option. Will have to search any tools which can help in this.
Also can any one suggest already available benchmarks for this kind of problem.
Any recommendation depends partially on where the script executing the queries resides. If the script executing the queries is on the same server as the MySQL instance then you won't see that much of a difference, but there will still be a small overhead in executing 200k queries compared to 1 stored procedure.
My advice either way would be though to make it as a stored procedure. You would need maybe a couple of procedures.
A procedure that combines the 10 statements you do per-operation
into 1 call
A procedure that can iterate over a table of arguments using a CURSOR to feed into procedure 1
Your process would be
Populate a table with arguments which would be fed into procedure 1 by procedure 2
Execute procedure 2
This would yield performance benefits as there is no need to connect to the MySQL server 20000*10 times. While the overhead per-request may be small, milliseconds add up. Even if the saving is 0.1ms per request, that's still 20 seconds saved.
Another option could be to modify your requests to perform all 20k data operations at once (if viable) by adjusting your 10 queries to pull data from the database table mentioned above. The key to all of this is to get the arguments loaded in a single batch insert, and then using statements on the MySQL server within a procedure to process them without further round trips.

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.

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.