mysql定時器event詳解
一. 事件簡介
事件(event)是
MySQL在相應的時刻調用的過程式數據庫對象。一個事件可調用一次,也可周期性的啟動,它由一個特定的線程來管理的,也就是所謂的“事件調度器”。
事件和觸發器類似,都是在某些事情發生的時候啟動。當數據庫上啟動一條語句的時候,觸發器就啟動了,而事件是根據調度事件來啟動的。由于他們彼此相似,所以事件也稱為臨時性觸發器。
事件取代了原先只能由操作系統的計劃任務來執行的工作,而且MySQL的事件調度器可以精確到每秒鐘執行一個任務,而操作系統的計劃任務(如:Linux下的CRON或Windows下的任務計劃)只能精確到每分鐘執行一次。
二. 事件的優缺點
2.1 優點
一些對數據定時性操作不再依賴外部程序,而直接使用數據庫本身提供的功能。
可以實現每秒鐘執行一個任務,這在一些對實時性要求較高的環境下就非常實用了。
2.2 缺點
定時觸發,不可以調用。
三. 創建事件
查看是否開啟evevt與開啟evevt。
MySQL evevt功能默認是關閉的,可以使用下面的語句來看evevt的狀態,如果是OFF或者0,表示是關閉的。
mysql> show VARIABLES LIKE 'event_schedu%';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| event_scheduler | OFF |
+-----------------+-------+
1 row in set (0.00 sec)
開啟event功能:
mysql> SET GLOBAL event_scheduler = 1;
Query OK, 0 rows affected (0.00 sec)
mysql> show VARIABLES LIKE 'event_schedu%';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| event_scheduler | ON |
+-----------------+-------+
1 row in set (0.00 sec)
創建event舉例:
mysql> use test-_T
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
先創建存儲過程:
mysql> delimiter //
mysql> create procedure del_proc()
-> begin
-> INSERT INTO req(entname) SELECT t.`NAME` FROM (SELECT NAME, min(ZCSJ) AS mydate FROM test-_T.t_user_sign GROUP BY NAME) t WHERE t.mydate > NOW() - 36 * 60 * 1000 and t.`NAME` not in(SELECT entname from req);
-> UPDATE req SET `STATUS` = '0' where `STATUS` is null;
-> end
-> //
Query OK, 0 rows affected (0.00 sec)
執行存儲過程的內容:
mysql> call del_proc();
-> //
Query OK, 2 rows affected, 1 warning (0.29 sec)
創建事件deldata_event(其作用:每隔一分鐘自動調用del_proc()存儲過程的內容)
mysql> create event deldata_event
-> on schedule every 1 minute
-> on completion preserve disable
-> do call del_proc();
-> //
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql>
開啟事件deldata_event
mysql> alter event deldata_event on completion preserve enable;
-> //
Query OK, 0 rows affected (0.00 sec)
關閉事件deldata_event
mysql>alter event deldata_event on completion preserve disable;
查看自己創建的event:
mysql> select * from mysql.event;
+--------------+---------------+-----------------+---------+------------+----------------+----------------+---------------------+---------------------+---------------------+---------------------+------+---------+---------------+--------------------------------------------+---------+------------+-----------+----------------------+----------------------+-----------------+-----------------+
| db | name | body | definer | execute_at | interval_value | interval_field | created | modified | last_executed | starts | ends | status | on_completion | sql_mode | comment | originator | time_zone | character_set_client | collation_connection | db_collation | body_utf8 |
+--------------+---------------+-----------------+---------+------------+----------------+----------------+---------------------+---------------------+---------------------+---------------------+------+---------+---------------+--------------------------------------------+---------+------------+-----------+----------------------+----------------------+-----------------+-----------------+
| Test-_T | deldata_event | call del_proc() | mdba@% | NULL | 1 | MINUTE | 2017-05-09 10:21:57 | 2017-05-09 10:22:11 | 2017-05-09 02:24:57 | 2017-05-09 02:21:57 | NULL | ENABLED | PRESERVE | STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION | | 15 | SYSTEM | utf8 | utf8_general_ci | utf8_general_ci | call del_proc() |
+--------------+---------------+-----------------+---------+------------+----------------+----------------+---------------------+---------------------+---------------------+---------------------+------+---------+---------------+--------------------------------------------+---------+------------+-----------+----------------------+----------------------+-----------------+-----------------+
1 row in set (0.00 sec)
MySQL中查看Event的執行情況
通過執行如下的語句:
SELECT * FROM information_schema.EVENTS;
例如:
mysql> SELECT * FROM information_schema.events \G
*************************** 1. row ***************************
EVENT_CATALOG: def
EVENT_SCHEMA: ixinnuo_sjcj
EVENT_NAME: deldata_event
DEFINER: mdba@%
TIME_ZONE: SYSTEM
EVENT_BODY: SQL
EVENT_DEFINITION: call del_proc()
EVENT_TYPE: RECURRING
EXECUTE_AT: NULL
INTERVAL_VALUE: 1
INTERVAL_FIELD: MINUTE
SQL_MODE: STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION
STARTS: 2017-05-09 10:21:57
ENDS: NULL
STATUS: ENABLED
ON_COMPLETION: PRESERVE
CREATED: 2017-05-09 10:21:57
LAST_ALTERED: 2017-05-09 10:22:11
LAST_EXECUTED: 2017-05-11 08:43:57
EVENT_COMMENT:
ORIGINATOR: 15
CHARACTER_SET_CLIENT: utf8
COLLATION_CONNECTION: utf8_general_ci
DATABASE_COLLATION: utf8_general_ci
1 row in set (0.00 sec)
可以看到相應的庫中的作業信息,其中LAST_EXECUTED字段會反映出相應的作業最近一次的執行時間