using the event scheduler: the friendly behind-the-scenes helper
TRANSCRIPT
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
1/43
MySQL 5.1Event Scheduler
The friendly behind thescenes helper
Andrey HristovSoftware Engineer
Giuseppe MaxiaMySQL Community Team Lead
Sun Microsystems
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
2/43
About us - Andrey "Poohie" HristovMySQL Software Engineer
MySQL Connectors Software Developer Author of
the Event Scheduler in MySQL 5.1Connector/C++Connector/OO.orgPHP drivers for MySQL
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
3/43
about us -Giuseppe Maxiaa.k.a. The Data Charmer
MySQL Community Team LeadLong time hacking with MySQL featuresFormerly, database consul ta nt, de signer, coder.A passion for QAAn even greater passion for open source... and communityPassionate blogger http://datacharmer.blogspot.com
http://datacharmer.blogspot.com/http://datacharmer.blogspot.com/http://datacharmer.blogspot.com/http://datacharmer.blogspot.com/http://datacharmer.blogspot.com/http://datacharmer.blogspot.com/http://datacharmer.blogspot.com/http://datacharmer.blogspot.com/http://datacharmer.blogspot.com/http://datacharmer.blogspot.com/http://datacharmer.blogspot.com/http://datacharmer.blogspot.com/http://datacharmer.blogspot.com/http://datacharmer.blogspot.com/http://datacharmer.blogspot.com/http://datacharmer.blogspot.com/http://datacharmer.blogspot.com/http://datacharmer.blogspot.com/http://datacharmer.blogspot.com/http://datacharmer.blogspot.com/http://datacharmer.blogspot.com/http://datacharmer.blogspot.com/ -
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
4/43
Updated presentation slides
you will find an up to date copy of these slides at:http://datacharmer.org/presentations/uc2009/
http://datacharmer.org/presentations/uc2009/http://datacharmer.org/presentations/uc2009/http://datacharmer.org/presentations/uc2009/ -
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
5/43
We love your feedback!Tell it to the Twitter community
#mysqlconf "your feedback here"Twitter me
@datacharmer "your feedback here"Post a comment on the MySQL ConferenceFacebook groupSend a public message (becomes a blog post)
Blog about it (if you already have a blog)Find my boss in the corridors and tell him I wasgreat!
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
6/43
MySQL 5.1 GA
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
7/43
MySQL 5.1 GA
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
8/43
MySQL 5.1 GA
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
9/43
MySQL 5.1 GA
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
10/43
MySQL 5.1 GA
We can answer the hardquestions
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
11/43
The event Scheduler
WHAT
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
12/43
What is the event scheduler Temporal triggers
NOT related to a specific tableExecute SQL code
at a given timeor at given intervals
Created by Andrey HristovFirst released with MySQL 5.1
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
13/43
How does it work?
MySQL Server
event scheduler thread
regular threadregular threadregular threadregular threadregular thread
eventtime?
event thread
start
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
14/43
The event Scheduler
WHY
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
15/43
Why using the event scheduler?Cross platform scheduler
No external applications neededNo overhead
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
16/43
The event Scheduler
HOW
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
17/43
How to use the event scheduler 1. Enable the event scheduler
A. in the option file event-scheduler=1B. online
SET GLOBAL event_scheduler=ON;
2. Create an event3. Check the effects
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
18/43
Event creation syntaxCREATE EVENT event_nameON SCHEDULE
AT {DATE AND TIME}DO
{SQL COMMAND};
CREATE EVENT event_nameON SCHEDULE
EVERY {X}{SECOND|MINUTE|HOUR|DAY|MONTH|YEAR|WEEK}
DO{SQL COMMAND};
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
19/43
Event creation syntaxCREATE EVENT event_nameON SCHEDULE {schedule clause}
[ON COMPLETION [NOT] PRESERVE][STARTS {DATE TIME}][ENDS {DATE TIME} ][ENABLE|DISABLE]
DO{SQL COMMAND};
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
20/43
Creating an event at a given timeCREATE EVENT event_nameON SCHEDULE AT '2009-04-21 15:55:00'DO
INSERT INTO some_table VALUES('gotcha', now());
CREATE EVENT event_nameON SCHEDULE AT now() + interval 20 minuteDOCALL smart_procedure()
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
21/43
Creating a recurring eventCREATE EVENT event_nameON SCHEDULE EVERY 20 MINUTEDO
INSERT INTO some_table VALUES('gotcha', now());
CREATE EVENT event_nameON SCHEDULE every 7 DAYDOCALL smart_procedure()
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
22/43
Creating a recurring eventCREATE EVENT event_nameON SCHEDULE EVERY 10 MINUTESTARTS NOW() + INTERVAL 2 HOUR ENDS NOW() + INTERVAL 4 HOUR DO
CALL some_procedure();
# creates an event that runs every# 10 minutes, but does not start now.# It will start in 2 hours# and end two hours later
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
23/43
looking for eventsSHOW EVENTS\G
Db: test
Name: e1Definer: msandbox@%Time zone: SYSTEM
Type: RECURRINGExecute at: NULL
Interval value: 20Interval field: MINUTE
Starts: 2009-04-04 22:16:24Ends: NULL
Status: ENABLEDOriginator: 0
character_set_client: latin1collation_connection: latin1_swedish_ci
Database Collation: latin1_swedish_ci
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
24/43
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
25/43
looking for eventsSELECT * FROM INFORMATION_SCHEMA.EVENTS\G
... SQL_MODE:STARTS: 2009-04-18 22:16:24
ENDS: NULLSTATUS: ENABLED
ON_COMPLETION: NOT PRESERVECREATED: 2009-04-18 22:14:54
LAST_ALTERED: 2009-04-18 22:16:23LAST_EXECUTED: 2009-04-18 22:16:24EVENT_COMMENT:
ORIGINATOR: 0
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
26/43
Altering eventsCREATE EVENT event_nameON SCHEDULE EVERY 20 MINUTESTARTS NOW() + INTERVAL 1 HOUR DO
INSERT INTO some_table VALUES('gotcha', now());
LTER EVENT event_nameON SCHEDULE EVERY 20 MINUTESTARTS NOW() + INTERVAL 1 MINUTE;
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
27/43
The event Scheduler
WHEN
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
28/43
When is the event scheduler useful?Data cleanup
Consistency checksRefreshing stale data
dropping old partitionscreating new ones
Extract-Transform-Load (ETL) operationsCreating summary tablesPrepare detail data for warehousing
Whenever you don't need to use external resources(email, operating system info)
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
29/43
The event Scheduler
CAVEATS
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
30/43
Something to be aware of Not preserved
By default, events are removed after their lastexecution.
The BINLOG_FORMAT is the one of the EventScheduler thread, not the one used in the client thatcreated it
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
31/43
Something to be aware of Errors
Errors go to the error log only (not visible to theclient that created the events)Factual errors detected at run time, not at creationtime
e.g. table not foundprocedure called with wrong parameters
WHAT TO DO: create a procedure, test it, andthen attach it to an event
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
32/43
Something to be aware of Replication
events creation are replicated with"slaveside_disabled"events actions are replicated
Compare with triggers wheretriggers are replicatedtrigger actions aren't
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
33/43
Something to be aware of The events scheduler cannot access the operatingsystem .Thus, the event scheduler CAN NOT
send emaillist directorieswrite to arbitrary filesrun applications
BUT, we have a hack for that. Stay with us
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
34/43
The event Scheduler
TRICKS
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
35/43
Hacking the event scheduler Purging the process list
Combine the event scheduler with MySQL Proxy andFederated tables to use Operating Systemcommands
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
36/43
operating system
operating system from the event scheduler (1)
M S L Server
eventscheduler
tabletabletable
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
37/43
operating system
operating system from the event scheduler (2)
M S L Server
eventscheduler
tabletabletable
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
38/43
operating system
operating system from the event scheduler (2)
M S L Server
eventscheduler
tabletabletable
federatedtable
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
39/43
operating system
operating system from the event scheduler (2)
M S L Server
eventscheduler
tabletabletable
federatedtable MySQL
Proxy
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
40/43
operating system
operating system from the event scheduler (2)
M S L Server
eventscheduler
tabletabletable
federatedtable MySQL
Proxy
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
41/43
The event Scheduler
HANDS ON
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
42/43
Live demosSimple events
Checking errorsAccessing the operating system from events
-
8/14/2019 Using the Event Scheduler: The Friendly Behind-the-Scenes Helper
43/43
Que stion time
Thanks!
Looking for the slides?http:// datacharmer.org/ presentations/uc2009/
http://datacharmer.org/presentations/uc2009/http://datacharmer.org/presentations/uc2009/http://datacharmer.org/presentations/uc2009/http://datacharmer.org/presentations/uc2009/http://datacharmer.org/presentations/uc2009/http://datacharmer.org/presentations/uc2009/http://datacharmer.org/presentations/uc2009/http://datacharmer.org/presentations/uc2009/