MYSQL事件排程器

pcsh發表於2010-09-06

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的排程器也可以排程儲存過程。

[@more@] IXPUB技術社群() 詳細出處參考:

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

相關文章