MySQL定時器十例演示
查看MySQL的版本號:
mysql> select version();
+------------+
| version() |
+------------+
| 5.6.39-log |
+------------+
1 row in set (0.00 sec)
創建一個記錄時間表
USE test;
CREATE TABLE test01 (timeline TIMESTAMP);
實例一:
查看MySQL的當前時間並且創建一個定時器,每20秒中插入一次當前的時間
select now(); CREATE EVENT e_test_insert ON SCHEDULE EVERY 20 SECOND STARTS TIMESTAMP ‘2018-09-15 09:20:00‘ DO INSERT INTO test.test01 VALUES (CURRENT_TIMESTAMP);
mysql> select now(); +---------------------+ | now() | +---------------------+ | 2018-09-15 09:16:59 | +---------------------+ 1 row in set (0.00 sec) mysql> CREATE EVENT e_test_insert -> ON SCHEDULE EVERY 20 SECOND STARTS TIMESTAMP ‘2018-09-15 09:20:00‘ -> DO INSERT INTO test.test01 VALUES (CURRENT_TIMESTAMP); Query OK, 0 rows affected (0.00 sec) mysql>
查看MySQL當前時間:
mysql> select now();
等待20秒鐘後,再執行查詢成功。
mysql> select * from test01;
+---------------------+
| timeline |
+---------------------+
| 2018-09-15 09:01:07 |
| 2018-09-15 09:01:27 |
+---------------------+
2 rows in set (0.00 sec)
實例二:
創建一個定時器test01,當前時間2分鐘後寫入數據到test01_lastmonth表,並且清空test01表:
select now();
CREATE EVENT test01
ON SCHEDULE AT TIMESTAMP ‘2018-09-15 09:30:00‘ + INTERVAL 2 MINUTE
do create table test.test01_lastmonth as select * from test.test01;TRUNCATE TABLE test.test01;
經測試
是新建了一個表test01_lastmonth,但是test01表沒有被清空,並且一直在寫入數據。說明上述的sql語句的寫法是有問題的。
於是采用下面的寫法是正確的。於是得出結論:采用下面的寫法定時器是可以同時添加多條sql語句作為計劃任務來執行的
delimiter $$
CREATE EVENT test02
ON SCHEDULE AT TIMESTAMP ‘2018-09-15 09:40:00‘ + INTERVAL 2 MINUTE
COMMENT ‘xiaowu create‘
do
BEGIN
create table test.test01_lastmonth as select * from test.test01;
TRUNCATE TABLE test.test01;
END $$
delimiter ;
此實例演示過程如下:
mysql> delimiter $$
mysql> CREATE EVENT test02
-> ON SCHEDULE AT TIMESTAMP ‘2018-09-15 09:40:00‘ + INTERVAL 2 MINUTE
-> COMMENT ‘xiaowu create‘
-> do
-> BEGIN
-> create table test.test01_lastmonth as select * from test.test01;
-> TRUNCATE TABLE test.test01;
-> END $$
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
此時查看新表還沒被創建,舊表test01數據還沒被清空
mysql> select now();select * from test01_lastmonth;select * from test01;
+---------------------+
| now() |
+---------------------+
| 2018-09-15 09:41:54 |
+---------------------+
1 row in set (0.00 sec)
ERROR 1146 (42S02): Table ‘test.test01_lastmonth‘ doesn‘t exist
+---------------------+
| timeline |
+---------------------+
| 2018-09-15 09:39:00 |
| 2018-09-15 09:39:20 |
| 2018-09-15 09:39:40 |
| 2018-09-15 09:40:00 |
| 2018-09-15 09:40:20 |
| 2018-09-15 09:40:40 |
| 2018-09-15 09:41:00 |
| 2018-09-15 09:41:20 |
| 2018-09-15 09:41:40 |
+---------------------+
42 rows in set (0.00 sec)
此時查看新表被創建,舊表test01數據被清空,並且已經插入一條數據
提示:舊表test01已經被清空了,但是之前的計劃事件e_test_insert每20秒插入一條記錄到test01表仍然在繼續執行
mysql> select now();select * from test01_lastmonth;select * from test01;
+---------------------+
| now() |
+---------------------+
| 2018-09-15 09:42:24 |
+---------------------+
1 row in set (0.00 sec)
+---------------------+
| timeline |
+---------------------+
| 2018-09-15 09:39:00 |
| 2018-09-15 09:39:20 |
| 2018-09-15 09:39:40 |
| 2018-09-15 09:40:00 |
| 2018-09-15 09:40:20 |
| 2018-09-15 09:40:40 |
| 2018-09-15 09:41:00 |
| 2018-09-15 09:41:20 |
| 2018-09-15 09:41:40 |
| 2018-09-15 09:42:00 |
+---------------------+
43 rows in set (0.00 sec)
+---------------------+
| timeline |
+---------------------+
| 2018-09-15 09:42:20 |
+---------------------+
1 row in set (0.00 sec)
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
再次總結測試實例二:
USE test;
CREATE TABLE test01 (timeline TIMESTAMP);
select now();
CREATE EVENT e_test_insert
ON SCHEDULE EVERY 20 SECOND STARTS TIMESTAMP ‘2018-09-15 10:10:00‘
DO INSERT INTO test.test01 VALUES (CURRENT_TIMESTAMP);
delimiter $$
CREATE EVENT test02
ON SCHEDULE AT TIMESTAMP ‘2018-09-15 10:10:00‘ + INTERVAL 2 MINUTE
COMMENT ‘xiaowu create‘
do
BEGIN
create table test.test01_lastmonth as select * from test.test01;
TRUNCATE TABLE test.test01;
END $$
delimiter ;
mysql> select now();select * from test01_lastmonth;select * from test01;
+---------------------+
| now() |
+---------------------+
| 2018-09-15 10:10:39 |
+---------------------+
1 row in set (0.00 sec)
ERROR 1146 (42S02): Table ‘test.test01_lastmonth‘ doesn‘t exist
+---------------------+
| timeline |
+---------------------+
| 2018-09-15 10:10:00 |
| 2018-09-15 10:10:20 |
+---------------------+
2 rows in set (0.00 sec)
mysql>
此時新表已經創建並且舊表test01數據被清空:
mysql> select now();select * from test01_lastmonth;select * from test01;
+---------------------+
| now() |
+---------------------+
| 2018-09-15 10:12:00 |
+---------------------+
1 row in set (0.00 sec)
+---------------------+
| timeline |
+---------------------+
| 2018-09-15 10:10:00 |
| 2018-09-15 10:10:20 |
| 2018-09-15 10:10:40 |
| 2018-09-15 10:11:00 |
| 2018-09-15 10:11:20 |
| 2018-09-15 10:11:40 |
| 2018-09-15 10:12:00 |
+---------------------+
7 rows in set (0.00 sec)
Empty set (0.00 sec)
此時被清空的舊表test01 20秒後已經開始插入數據了
提示:舊表test01已經被清空了,但是之前的計劃事件e_test_insert每20秒插入一條記錄到test01表仍然在繼續執行
mysql> select now();select * from test01_lastmonth;select * from test01;
+---------------------+
| now() |
+---------------------+
| 2018-09-15 10:12:37 |
+---------------------+
1 row in set (0.00 sec)
+---------------------+
| timeline |
+---------------------+
| 2018-09-15 10:10:00 |
| 2018-09-15 10:10:20 |
| 2018-09-15 10:10:40 |
| 2018-09-15 10:11:00 |
| 2018-09-15 10:11:20 |
| 2018-09-15 10:11:40 |
| 2018-09-15 10:12:00 |
+---------------------+
7 rows in set (0.00 sec)
+---------------------+
| timeline |
+---------------------+
| 2018-09-15 10:12:20 |
+---------------------+
1 row in set (0.00 sec)
實例三:定時每天的某個時間點來清空表test03
定時每天的上午10:45:00清空表test03
DROP EVENT IF EXISTS test03;
CREATE EVENT test03
ON SCHEDULE EVERY 1 day STARTS date_add(concat(current_date(), ‘ 10:45:00‘), interval 0 second)
ON COMPLETION PRESERVE ENABLE
DO TRUNCATE TABLE test01;
定時每天的下午18:00:00清空表test03
DROP EVENT IF EXISTS test04;
CREATE EVENT test04
ON SCHEDULE EVERY 1 day STARTS date_add(concat(current_date(), ‘ 18:00:00‘), interval 0 second)
ON COMPLETION PRESERVE ENABLE
DO TRUNCATE TABLE test03;
實例四:定時每天的上午11:00:00清空表test01
CREATE EVENT test05
ON SCHEDULE EVERY 1 day STARTS TIMESTAMP (current_date(),‘11:00:00‘)
DO TRUNCATE TABLE test.test01;
查看創建事件調度器語句:
mysql> show create event test05\G
*************************** 1. row ***************************
Event: test05
sql_mode: STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION
time_zone: SYSTEM
Create Event: CREATE DEFINER=`root`@`localhost` EVENT `test05` ON SCHEDULE EVERY 1 DAY STARTS ‘2018-09-15 11:00:00‘ ON COMPLETION NOT PRESERVE ENABLE DO TRUNCATE TABLE test.test01
character_set_client: utf8
collation_connection: utf8_general_ci
Database Collation: utf8_general_ci
1 row in set (0.00 sec)
實例五:
2018年7月20日12點整清空aaa表:
CREATE EVENT e1_test
ON SCHEDULE AT TIMESTAMP ‘2018-07-20 12:00:00‘
DO TRUNCATE TABLE test.aaa;
實例六:
5天後開啟每天定時清空aaa表:
CREATE EVENT e2_test
ON SCHEDULE EVERY 1 DAY
STARTS CURRENT_TIMESTAMP + INTERVAL 5 DAY
DO TRUNCATE TABLE test.aaa;
實例七:
每天定時清空test表,5天後停止執行:
CREATE EVENT e3_test
ON SCHEDULE EVERY 1 DAY
ENDS CURRENT_TIMESTAMP + INTERVAL 5 DAY
DO TRUNCATE TABLE test.aaa;
實例八:
5天後開啟每天定時清空test表,一個月後停止執行:
CREATE EVENT e4_test
ON SCHEDULE EVERY 1 DAY
STARTS CURRENT_TIMESTAMP + INTERVAL 5 DAY
ENDS CURRENT_TIMESTAMP + INTERVAL 1 MONTH
DO TRUNCATE TABLE test.aaa;
[ON COMPLETION [NOT] PRESERVE]可以設置這個事件是執行一次還是持久執行,默認為NOT PRESERVE。
實例九:
每天定時清空test表(只執行一次,任務完成後就終止該事件):
CREATE EVENT e5_test
ON SCHEDULE EVERY 1 DAY
ON COMPLETION NOT PRESERVE
DO TRUNCATE TABLE test.aaa;
[ENABLE | DISABLE]可是設置該事件創建後狀態是否開啟或關閉,默認為ENABLE。
[COMMENT ‘comment’]可以給該事件加上註釋。
實例十:
修改事件(ALTER EVENT)
ALTER EVENT event_name
[ON SCHEDULE schedule]
[RENAME TO new_event_name]
[ON COMPLETION [NOT] PRESERVE]
[COMMENT ‘comment‘]
[ENABLE | DISABLE]
[DO sql_statement]
將每天清空test表改為5天清空一次:
ALTER EVENT e_test
ON SCHEDULE EVERY 5 DAY;
MySQL定時器十例演示