一. 事件简介
事件(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字段会反映出相应的作业最近一次的执行时间