文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>MYSQL事件调度器

MYSQL事件调度器

时间:2010-09-06  来源:pcsh

1.查看是否开启:

mysql> show variables like 'event_scheduler';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| event_scheduler | ON |
+-----------------+-------+
1 row in set (0.01 sec)

mysql> select @@event_scheduler;
+-------------------+
| @@event_scheduler |
+-------------------+
| ON |
+-------------------+
1 row in set (0.00 sec)

mysql> show processlist;

+-------+-----------------+----------------+-----------+---------+------+-------
----------------------+------------------+
| Id | User | Host | db | Command | Time | State
| Info |
+-------+-----------------+----------------+-----------+---------+------+-------
----------------------+------------------+
| 1 | event_scheduler | localhost | NULL | Daemon | 24 | Waitin
g for next activation | NULL |

2.语法

CREATE EVENT [IF NOT EXISTS] event_name
ON SCHEDULE schedule
[ON COMPLETION [NOT] PRESERVE]
[ENABLE | DISABLE]
[COMMENT 'comment']
DO sql_statement;
schedule:
AT TIMESTAMP [+ INTERVAL INTERVAL]
| EVERY INTERVAL [STARTS TIMESTAMP] [ENDS TIMESTAMP]
INTERVAL:
quantity {YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE |
WEEK | SECOND | YEAR_MONTH | DAY_HOUR | DAY_MINUTE |
DAY_SECOND | HOUR_MINUTE | HOUR_SECOND | MINUTE_SECOND}

例:

每秒插入一条记录到数据表
USE test;
CREATE TABLE aaa (timeline TIMESTAMP);
CREATE EVENT e_test_insert
ON SCHEDULE EVERY 1 SECOND
DO INSERT INTO test.aaa VALUES (CURRENT_TIMESTAMP);

5秒(天)后清空test表
CREATE EVENT e_test
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 5 SECOND
DO TRUNCATE TABLE test.aaa;
CREATE EVENT e_test
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 5 DAY
DO TRUNCATE TABLE test.aaa;

2008年5月23日9点39分20秒整清空test表
CREATE EVENT e_test
ON SCHEDULE AT TIMESTAMP '2008-05-23 9:39:20'
DO TRUNCATE TABLE test.aaa;

每天定时清空test表
CREATE EVENT e_test
ON SCHEDULE EVERY 1 DAY
DO TRUNCATE TABLE test.aaa;

5天后开启每天定时清空test表
CREATE EVENT e_test
ON SCHEDULE EVERY 1 DAY
STARTS CURRENT_TIMESTAMP + INTERVAL 5 DAY
DO TRUNCATE TABLE test.aaa;
这里5天也可以为0天,当时就开启清空表

每天定时清空test表,5天后停止执行
CREATE EVENT e_test
ON SCHEDULE EVERY 1 DAY
ENDS CURRENT_TIMESTAMP + INTERVAL 5 DAY
DO TRUNCATE TABLE test.aaa;

该设置要求天数大于1,否则报错。而且创建不成功

5天后开启每天定时清空test表,一个月后停止执行
CREATE EVENT e_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。
该事件会停止每隔一秒插入数据的事件,感觉这点上mysql做的还是有问题。

每天定时清空test表(只执行一次,任务完成后就终止该事件)
CREATE EVENT e_test
ON SCHEDULE EVERY 1 DAY
ON COMPLETION NOT PRESERVE
DO TRUNCATE TABLE test.aaa;
[ENABLE | DISABLE]可是设置该事件创建后状态是否开启或关闭,默认为ENABLE。
[COMMENT ‘comment’]可以给该事件加上注释。

3 定时服务日常维护测试
5.4.1 修改事件(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]
a、临时关闭事件
ALTER EVENT e_test DISABLE;
b、开启事件
ALTER EVENT e_test ENABLE;
c、将每天清空test表改为5天清空一次:
ALTER EVENT e_test
ON SCHEDULE EVERY 5 DAY;
d、重命名事件并加上注释
alter event test.new_e_test rename to e_test comment 'e_test_cm';
5.4.2 删除事件(DROP EVENT)
语法很简单,如下所示:
DROP EVENT [IF EXISTS] event_name例如删除前面创建的e_test事件
DROP EVENT e_test;当然前提是这个事件存在,否则会产生ERROR 1513 (HY000): Unknown event错误,因此最好加上IF EXISTS
DROP EVENT IF EXISTS e_test;
5.4.3 查看事件
a、查看一个event的详细信息可以用下面的视图:
SELECT * FROM INFORMATION_SCHEMA.EVENTS WHERE EVENT_NAME = 'e_test_insert' AND EVENT_SCHEMA = 'test'G;
b、简要列出所有的event:show events
语法:
SHOW EVENTS [FROM schema_name]
[LIKE 'pattern' | WHERE expr]
格式化显示所有event
SHOW EVENTSG
格式化显示test用户的event
show events FROM test;
c、查看event的创建信息
SHOW CREATE EVENT event_name
show create event test.e_testG
5.5、结论
该特性确实非常有用,可作为定时清空数据表、监控主从服务器、汇总数据到另一张表等等,并且可以精确到每秒,实时性也可以得到保障。
不过如果当两个事件的针对相同的对象的时候,会出现冲突,这种情况还不明确是我理解的问题还是确实是这样,比如每秒插入和定时删除就会冲突。除了调度SQL语句之外,MYSQL的调度器也可以调度存储过程。


IXPUB技术社区(www.ixpub.net) 详细出处参考:http://www.ixpub.net/viewthread.php?tid=874384

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载