MySQL籌劃義務(事宜調劑器) Event Scheduler引見。本站提示廣大學習愛好者:(MySQL籌劃義務(事宜調劑器) Event Scheduler引見)文章只能為提供參考,不一定能成為您想要的結果。以下是MySQL籌劃義務(事宜調劑器) Event Scheduler引見正文
要檢查以後能否已開啟事宜調劑器,可履行以下SQL:
SHOW VARIABLES LIKE 'event_scheduler';
或
SELECT @@event_scheduler;
或
SHOW PROCESSLIST;
若顯示:
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| event_scheduler | OFF |
+-----------------+-------+
則可履行
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
創立事宜(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秒鐘後,再履行查詢勝利。
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;