MySQL准時器開啟、挪用完成代碼。本站提示廣大學習愛好者:(MySQL准時器開啟、挪用完成代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是MySQL准時器開啟、挪用完成代碼正文
創立測試表
CREATE TABLE t
(
v VARCHAR(100) NOT NULL
) ENGINE INNODB DEFAULT CHARSET=utf8;
創立准時器 挪用的存儲進程
DELIMITER $$
DROP PROCEDURE IF EXISTS e_test $$
CREATE PROCEDURE e_test()
BEGIN
INSERT INTO t VALUES('1');
END $$
DELIMITER ;
要使准時起感化 MySQL的常量GLOBAL event_scheduler必需為on或許是1
-- 檢查能否開啟准時器
SHOW VARIABLES LIKE '%sche%';
-- 開啟准時器 0:off 1:on
SET GLOBAL event_scheduler = 1;
-- 創立事宜
--每隔一秒主動挪用e_test()存儲進程
CREATE EVENT IF NOT EXISTS event_test
ON SCHEDULE EVERY 1 SECOND
ON COMPLETION PRESERVE
DO CALL e_test();
-- 開啟事宜
ALTER EVENT event_test ON
COMPLETION PRESERVE ENABLE;
-- 封閉事宜
ALTER EVENT event_test ON
COMPLETION PRESERVE DISABLE;
從如今開端每隔九天准時履行
CREATE EVENT EVENT1
ON SCHEDULE EVERY 9 DAY STARTS NOW()
ON COMPLETION PRESERVE ENABLE
DO
BEGIN
CALL TOTAL();
END
每一個月的一號清晨1 點履行
CREATE EVENT EVENT2
ON SCHEDULE EVERY 1 MONTH STARTS DATE_ADD(DATE_ADD(DATE_SUB(CURDATE(),INTERVAL DAY(CURDATE())-1 DAY), INTERVAL 1 MONTH),INTERVAL 1 HOUR)
ON COMPLETION PRESERVE ENABLE
DO
BEGIN
CALL STAT();
END
每一個季度一號的清晨2點履行
CREATE EVENT TOTAL_SEASON_EVENT
ON SCHEDULE EVERY 1 QUARTER STARTS DATE_ADD(DATE_ADD(DATE( CONCAT(YEAR(CURDATE()),'-',ELT(QUARTER(CURDATE()),1,4,7,10),'-',1)),INTERVAL 1 QUARTER),INTERVAL 2 HOUR)
ON COMPLETION PRESERVE ENABLE
DO
BEGIN
CALL SEASON_STAT();
END
每一年1月1號清晨四點履行
CREATE EVENT TOTAL_YEAR_EVENT
ON SCHEDULE EVERY 1 YEAR STARTS DATE_ADD(DATE(CONCAT(YEAR(CURDATE()) + 1,'-',1,'-',1)),INTERVAL 4 HOUR)
ON COMPLETION PRESERVE ENABLE
DO
BEGIN
CALL YEAR_STAT();
END
MySQL的event在停止按月,季,年停止主動挪用存儲進程時,為了測試可以把體系改成年的最初一天,如2010-12-31 23:59:55;