mysql計劃任務:event schedule

season0891發表於2012-05-08

mysql的event schedule 可以讓你設定你的mysql資料庫再某段時間執行你想要的動作

這與檢視是不相同的。

create event test1
on schedule every 1 day
starts  '2007-09-01 12:00:00'
on completion not preserve
do insert into yyy values('hhh','uuu');


create event test
ON SCHEDULE AT '2007-09-01 12:00:00' + INTERVAL 1 DAY
on completion not preserve
do insert into yyy values('hhh','uuu');

解釋:從2007-09-01開始,每天對錶yyy在12:00:00進行一個插入操作。而且只執行一次(on completion not preserve )

 

使用這個功能之前必須確保event_scheduler已開啟,可執行

SET GLOBAL event_scheduler = 1;

SET GLOBAL event_scheduler = ON;
來開啟,也可以直接在啟動命令加上“–event_scheduler=1”,例如:

mysqld ... --event_scheduler=1

 

my.ini or my.cnf 中的
[mysqld]
新增 event_scheduler=ON

 

要檢視當前是否已開啟事件排程器,可執行如下SQL:

SHOW VARIABLES LIKE 'event_scheduler';

SELECT @@event_scheduler;

SHOW PROCESSLIST;
二、建立事件(CREATE EVENT)
先來看一下它的語法:

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}
1) 首先來看一個簡單的例子來演示每秒插入一條記錄到資料表

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);
等待3秒鐘後,再執行查詢看看:

mysql> SELECT * FROM aaa;
+---------------------+
| timeline            |
+---------------------+
| 2007-07-18 20:44:26 |
| 2007-07-18 20:44:27 |
| 2007-07-18 20:44:28 |
+---------------------+
2) 5天后清空test表:

CREATE EVENT e_test
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 5 DAY
DO TRUNCATE TABLE test.aaa;
3) 2007年7月20日12點整清空test表:

CREATE EVENT e_test
ON SCHEDULE AT TIMESTAMP '2007-07-20 12:00:00'
DO TRUNCATE TABLE test.aaa;
4) 每天定時清空test表:

CREATE EVENT e_test
ON SCHEDULE EVERY 1 DAY
DO TRUNCATE TABLE test.aaa;
5) 5天后開啟每天定時清空test表:

CREATE EVENT e_test
ON SCHEDULE EVERY 1 DAY
STARTS CURRENT_TIMESTAMP + INTERVAL 5 DAY
DO TRUNCATE TABLE test.aaa;
6) 每天定時清空test表,5天后停止執行:

CREATE EVENT e_test
ON SCHEDULE EVERY 1 DAY
ENDS CURRENT_TIMESTAMP + INTERVAL 5 DAY
DO TRUNCATE TABLE test.aaa;
7) 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。
8) 每天定時清空test表(只執行一次,任務完成後就終止該事件):

CREATE EVENT e_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]
1) 臨時關閉事件

ALTER EVENT e_test DISABLE;
2) 開啟事件

ALTER EVENT e_test ENABLE;
3) 將每天清空test表改為5天清空一次:

ALTER EVENT e_test
ON SCHEDULE EVERY 5 DAY;
四、刪除事件(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;

come from:http://blog.csdn.net/jamesjun/article/details/3096974

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/90618/viewspace-723005/,如需轉載,請註明出處,否則將追究法律責任。

相關文章