我怎样才能让 MySQL 记录预定事件?
How can i have MySQL log scheduled Events?
我创建了一个 Event,但不知道如何记录它何时运行、需要多长时间以及是否有任何错误。我该怎么做?
CREATE EVENT ON SCHEDULE EVERY 5 MINUTE
DO BEGIN
...do something...
END
SELECT * FROM INFORMATION_SCHEMA.EVENTS
会帮助你
我将以下内容用于事件绩效报告。
请注意,它可以处理任意数量的单独事件(例如,您单独编码的 N 个事件)。
您在活动中作为步骤执行的操作由您决定。我在下面的事件中引用了此处未显示的 table 以及我在针对该业务定制的事件中所做的事情。显示所有这些会使这个答案太长。如果您是事件程序员,您会发现您的用途。
此外,我的事件中的 WHILE
循环可能不是最好的,除非您编写一些没有它们的简单事件。如果您不能安全地退出 WHILE,您的活动将永远 运行。所以请记住这一点。
架构存根
下面的 table 被靠近其代码顶部的事件用于对其执行插入,其唯一目的是获得唯一的 id
返回以用于插入到日志 table。用日期时间列等扩展它。它显示了一个普通的 usedBy
列,至少可以捕获某些内容,例如事件名称。主要是,它想要从它返回一个分配的 auto_increment (id
)。
drop table if exists incarnations;
create table incarnations
( -- NoteA
-- a control table used to feed incarnation id's to events that want performance reporting.
-- The long an short of it, insert a row here merely to acquire an auto_increment id
id int auto_increment primary key,
usedBy varchar(50) not null
-- could use other columns perhaps, like how used or a datetime
-- but mainly it feeds back an auto_increment
-- the usedBy column is like a dummy column just to be fed a last_insert_id()
-- but the insert has to insert something, so we use usedBy
);
通用日志记录 table 如下:
drop table if exists EvtsLog;
create table EvtsLog
( id int auto_increment primary key,
incarnationId int not null, -- See NoteA (above)
evtName varchar(20) not null, -- allows for use of this table by multiple events
step int not null, -- facilitates reporting on event level performance
debugMsg varchar(1000) not null,
dtWhenLogged datetime not null
-- tweak this with whatever indexes your can bear to have
-- run maintenance on this table to rid it of unwanted rows periodically
-- as it impacts performance. So, dog the rows out to an archive table or whatever.
);
示例事件
-- Now the event kicks in on the server on its interval schedule and processes the batch.
-- But we need to modify that Event code because prior all it did was write a row to the log table
-- saying it was looking around. But it didn't actually do anything
drop event if exists `Every_2_Minutes_QuestionUpdateImport`;
DELIMITER $$
CREATE EVENT `Every_2_Minutes_QuestionUpdateImport`
ON SCHEDULE EVERY 2 MINUTE STARTS '2015-09-01 00:00:00'
ON COMPLETION PRESERVE
DO BEGIN
DECLARE bContinue bool default true;
DECLARE counter int default 0;
DECLARE logMsg varchar(1000);
DECLARE incarnationId int default 0;
DECLARE evtAlias varchar(20);
-- right here you could save `now()` into a variable, let's call it STARTEVT
set evtAlias:='ev2minQUI'; -- a shorter unique alias name, max 20 chars
-- Below we must acquire our incarnation id from the control table used for all events
-- that want to use it. It facilitates performance reporting with the use of the `steps` column and the datetime
-- that are put in the EvtsLog table
insert incarnations(usedBy) values (evtAlias); -- see NoteA
select last_insert_id() into incarnationId; -- an auto_increment handed to us by the control table
insert EvtsLog(incarnationId,evtName,step,debugMsg,dtWhenLogged)
select incarnationId,evtAlias,1,'Event Fired, begin looking',now(); -- 1: we are beginning
WHILE bContinue DO -- this is an intermediate-level skills event example. Be careful or you are stuck in the event forever
select min(batchId) into @idToPerform
from EvtsQuestionsToImportBatchHandler -- table not shown in this post on Whosebug
where batchStatus=1; -- @idToPerform, a variable, will be null if there is no such row
insert EvtsLog(incarnationId,evtName,step,debugMsg,dtWhenLogged)
select incarnationId,evtAlias,5,'Debug Place 1',now();
IF (@idToPerform IS NOT NULL) THEN
-- This next update line is very crucial, to mark the batch as underway and never picked up again
-- at the top of this WHILE loop (otherwise you may be stuck in here forever)
update EvtsQuestionsToImportBatchHandler set batchStatus=2,dtProcessBegan=now() where batchId=@idToPerform;
set counter:=counter+1; -- used outside of the while loop in the end
insert EvtsLog(incarnationId,evtName,step,debugMsg,dtWhenLogged)
select incarnationId,evtAlias,10,"a message maybe from concat and variables",now();
--
-- Here is where you actually do something
-- Here is where you actually do something
-- Here is where you actually do something
insert EvtsLog(incarnationId,evtName,step,debugMsg,dtWhenLogged)
select incarnationId,evtAlias,20,"a message maybe from concat and variables",now();
-- Here is where you actually do something
-- Here is where you actually do something
-- Here is where you actually do something
insert EvtsLog(incarnationId,evtName,step,debugMsg,dtWhenLogged)
select incarnationId,evtAlias,30,"a message maybe from concat and variables",now();
-- mark this batch as done:
update EvtsQuestionsToImportBatchHandler set batchStatus=3,dtProcessFinished=now() where batchId=@idToPerform;
ELSE
set bContinue=false; -- we are done with the event loop
END IF;
-- if bContinue is true, we will seek the next batch to process that has batchStatus=1, if there is one
-- right here a new now() could be performed, and a timediff() against the STARTEVT
-- to determine if you want to bail out also with a " set bContinue=false; "
END WHILE; -- this is an intermediate-level skills event example. Be careful or you are stuck in the event forever
-- this msg is crucial to see in the log table to know you are not locking in an endless WHILE loop
set logMsg:=concat("Number of batches processed=",counter); -- concat example (could have been used above)
insert EvtsLog(incarnationId,evtName,step,debugMsg,dtWhenLogged)
select incarnationId,evtAlias,90,logMsg,now(); -- 90: we are almost done
insert EvtsLog(incarnationId,evtName,step,debugMsg,dtWhenLogged)
select incarnationId,evtAlias,99,'Event Done',now(); -- 99: we are done
END$$
DELIMITER ; -- sets the delimiter back to what we are used to, the semi-colon
自然地,您将获得针对日志 table 的性能报告,其中包括 incarnationId
、evtName
、step
和日期时间。以下查询将多行事件信息折叠为每个事件 1 行 运行,具有时间差。
select incarnationId,dtBegin,dtEnd,TIMESTAMPDIFF(SECOND,dtBegin,dtEnd) as secDiff
from
( select incarnationId,min(dtBegin) as dtBegin,max(dtEnd) as dtEnd
from
( select incarnationId,
case
when step=1 then dtWhenLogged
end as dtBegin,
case
when step=99 then dtWhenLogged
end as dtEnd
from evtsLog
where evtName='evtName01'
) d1
group by incarnationId
) d2;
+---------------+---------------------+---------------------+---------+
| incarnationId | dtBegin | dtEnd | secDiff |
+---------------+---------------------+---------------------+---------+
| 101 | 2016-05-01 14:02:00 | 2016-05-01 14:02:01 | 1 |
| 102 | 2016-05-01 14:02:01 | 2016-05-01 14:02:07 | 6 |
+---------------+---------------------+---------------------+---------+
要获得更精确的微秒报告,MySQL 需要 5.6.4 或更高版本。看到这个 answer.
事件很难编写,因为没有与之关联的用户体验。通过使用日志记录 table,您不仅可以实现性能报告,还可以在开发过程中通过调试消息获得洞察力。
不要忘记 p运行e 记录日志 table 以控制其大小。也许将它存档在某个地方(也许通过另一个事件!)。
我创建了一个 Event,但不知道如何记录它何时运行、需要多长时间以及是否有任何错误。我该怎么做?
CREATE EVENT ON SCHEDULE EVERY 5 MINUTE
DO BEGIN
...do something...
END
SELECT * FROM INFORMATION_SCHEMA.EVENTS
会帮助你
我将以下内容用于事件绩效报告。
请注意,它可以处理任意数量的单独事件(例如,您单独编码的 N 个事件)。
您在活动中作为步骤执行的操作由您决定。我在下面的事件中引用了此处未显示的 table 以及我在针对该业务定制的事件中所做的事情。显示所有这些会使这个答案太长。如果您是事件程序员,您会发现您的用途。
此外,我的事件中的 WHILE
循环可能不是最好的,除非您编写一些没有它们的简单事件。如果您不能安全地退出 WHILE,您的活动将永远 运行。所以请记住这一点。
架构存根
下面的 table 被靠近其代码顶部的事件用于对其执行插入,其唯一目的是获得唯一的 id
返回以用于插入到日志 table。用日期时间列等扩展它。它显示了一个普通的 usedBy
列,至少可以捕获某些内容,例如事件名称。主要是,它想要从它返回一个分配的 auto_increment (id
)。
drop table if exists incarnations;
create table incarnations
( -- NoteA
-- a control table used to feed incarnation id's to events that want performance reporting.
-- The long an short of it, insert a row here merely to acquire an auto_increment id
id int auto_increment primary key,
usedBy varchar(50) not null
-- could use other columns perhaps, like how used or a datetime
-- but mainly it feeds back an auto_increment
-- the usedBy column is like a dummy column just to be fed a last_insert_id()
-- but the insert has to insert something, so we use usedBy
);
通用日志记录 table 如下:
drop table if exists EvtsLog;
create table EvtsLog
( id int auto_increment primary key,
incarnationId int not null, -- See NoteA (above)
evtName varchar(20) not null, -- allows for use of this table by multiple events
step int not null, -- facilitates reporting on event level performance
debugMsg varchar(1000) not null,
dtWhenLogged datetime not null
-- tweak this with whatever indexes your can bear to have
-- run maintenance on this table to rid it of unwanted rows periodically
-- as it impacts performance. So, dog the rows out to an archive table or whatever.
);
示例事件
-- Now the event kicks in on the server on its interval schedule and processes the batch.
-- But we need to modify that Event code because prior all it did was write a row to the log table
-- saying it was looking around. But it didn't actually do anything
drop event if exists `Every_2_Minutes_QuestionUpdateImport`;
DELIMITER $$
CREATE EVENT `Every_2_Minutes_QuestionUpdateImport`
ON SCHEDULE EVERY 2 MINUTE STARTS '2015-09-01 00:00:00'
ON COMPLETION PRESERVE
DO BEGIN
DECLARE bContinue bool default true;
DECLARE counter int default 0;
DECLARE logMsg varchar(1000);
DECLARE incarnationId int default 0;
DECLARE evtAlias varchar(20);
-- right here you could save `now()` into a variable, let's call it STARTEVT
set evtAlias:='ev2minQUI'; -- a shorter unique alias name, max 20 chars
-- Below we must acquire our incarnation id from the control table used for all events
-- that want to use it. It facilitates performance reporting with the use of the `steps` column and the datetime
-- that are put in the EvtsLog table
insert incarnations(usedBy) values (evtAlias); -- see NoteA
select last_insert_id() into incarnationId; -- an auto_increment handed to us by the control table
insert EvtsLog(incarnationId,evtName,step,debugMsg,dtWhenLogged)
select incarnationId,evtAlias,1,'Event Fired, begin looking',now(); -- 1: we are beginning
WHILE bContinue DO -- this is an intermediate-level skills event example. Be careful or you are stuck in the event forever
select min(batchId) into @idToPerform
from EvtsQuestionsToImportBatchHandler -- table not shown in this post on Whosebug
where batchStatus=1; -- @idToPerform, a variable, will be null if there is no such row
insert EvtsLog(incarnationId,evtName,step,debugMsg,dtWhenLogged)
select incarnationId,evtAlias,5,'Debug Place 1',now();
IF (@idToPerform IS NOT NULL) THEN
-- This next update line is very crucial, to mark the batch as underway and never picked up again
-- at the top of this WHILE loop (otherwise you may be stuck in here forever)
update EvtsQuestionsToImportBatchHandler set batchStatus=2,dtProcessBegan=now() where batchId=@idToPerform;
set counter:=counter+1; -- used outside of the while loop in the end
insert EvtsLog(incarnationId,evtName,step,debugMsg,dtWhenLogged)
select incarnationId,evtAlias,10,"a message maybe from concat and variables",now();
--
-- Here is where you actually do something
-- Here is where you actually do something
-- Here is where you actually do something
insert EvtsLog(incarnationId,evtName,step,debugMsg,dtWhenLogged)
select incarnationId,evtAlias,20,"a message maybe from concat and variables",now();
-- Here is where you actually do something
-- Here is where you actually do something
-- Here is where you actually do something
insert EvtsLog(incarnationId,evtName,step,debugMsg,dtWhenLogged)
select incarnationId,evtAlias,30,"a message maybe from concat and variables",now();
-- mark this batch as done:
update EvtsQuestionsToImportBatchHandler set batchStatus=3,dtProcessFinished=now() where batchId=@idToPerform;
ELSE
set bContinue=false; -- we are done with the event loop
END IF;
-- if bContinue is true, we will seek the next batch to process that has batchStatus=1, if there is one
-- right here a new now() could be performed, and a timediff() against the STARTEVT
-- to determine if you want to bail out also with a " set bContinue=false; "
END WHILE; -- this is an intermediate-level skills event example. Be careful or you are stuck in the event forever
-- this msg is crucial to see in the log table to know you are not locking in an endless WHILE loop
set logMsg:=concat("Number of batches processed=",counter); -- concat example (could have been used above)
insert EvtsLog(incarnationId,evtName,step,debugMsg,dtWhenLogged)
select incarnationId,evtAlias,90,logMsg,now(); -- 90: we are almost done
insert EvtsLog(incarnationId,evtName,step,debugMsg,dtWhenLogged)
select incarnationId,evtAlias,99,'Event Done',now(); -- 99: we are done
END$$
DELIMITER ; -- sets the delimiter back to what we are used to, the semi-colon
自然地,您将获得针对日志 table 的性能报告,其中包括 incarnationId
、evtName
、step
和日期时间。以下查询将多行事件信息折叠为每个事件 1 行 运行,具有时间差。
select incarnationId,dtBegin,dtEnd,TIMESTAMPDIFF(SECOND,dtBegin,dtEnd) as secDiff
from
( select incarnationId,min(dtBegin) as dtBegin,max(dtEnd) as dtEnd
from
( select incarnationId,
case
when step=1 then dtWhenLogged
end as dtBegin,
case
when step=99 then dtWhenLogged
end as dtEnd
from evtsLog
where evtName='evtName01'
) d1
group by incarnationId
) d2;
+---------------+---------------------+---------------------+---------+
| incarnationId | dtBegin | dtEnd | secDiff |
+---------------+---------------------+---------------------+---------+
| 101 | 2016-05-01 14:02:00 | 2016-05-01 14:02:01 | 1 |
| 102 | 2016-05-01 14:02:01 | 2016-05-01 14:02:07 | 6 |
+---------------+---------------------+---------------------+---------+
要获得更精确的微秒报告,MySQL 需要 5.6.4 或更高版本。看到这个 answer.
事件很难编写,因为没有与之关联的用户体验。通过使用日志记录 table,您不仅可以实现性能报告,还可以在开发过程中通过调试消息获得洞察力。
不要忘记 p运行e 记录日志 table 以控制其大小。也许将它存档在某个地方(也许通过另一个事件!)。