Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

Como posso ter eventos agendados de log do MySQL?


Eu uso o seguinte para o Relatório de Desempenho de Eventos.

Observe que ele pode lidar com tantos eventos separados (por exemplo, N eventos que você codifica separadamente) que você deseja lançar nele.

O que você faz como Passos dentro do seu Evento depende de você. Eu tenho uma referência no Evento abaixo para uma tabela não mostrada aqui, bem como o que eu faço no Evento que é personalizado para este negócio. Mostrar tudo isso tornaria essa resposta muito longa. Se você é um programador de eventos, encontrará seu usar para isso.

Além disso, o WHILE loop no meu evento pode não ser o melhor até que você codifique alguns eventos simples sem eles. Se você não sair com segurança do WHILE, seu evento será executado para sempre. Portanto, mantenha isso em mente se houver alguma coisa aqui.

Toco de esquema


A tabela a seguir é usada por um evento próximo ao topo de seu código para realizar uma inserção nele, com o único propósito de obter um id exclusivo de volta para uso em inserções na tabela de log. Expanda-o com uma coluna de data e hora ou algo semelhante. Ele mostra um usedBy trivial coluna para capturar algo pelo menos, como o nome do evento. Principalmente, ele quer um auto_increment atribuído de volta (o 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
);

Uma tabela de log genérica está abaixo:
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.
);

Um exemplo de evento

-- 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 Stackoverflow
        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 [email protected];

            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 [email protected];
        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

Naturalmente, você obtém o Relatório de desempenho na tabela de log que inclui incarnationId , evtName , step , e a data e hora. A consulta abaixo reduz as informações de eventos de várias linhas para 1 linha por execução de evento, com uma diferença de horário.
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 |
+---------------+---------------------+---------------------+---------+

Para relatórios mais precisos em microssegundos, é necessário o MySQL 5.6.4 ou superior. Veja esta resposta .

Os eventos são difíceis de escrever, pois não há UX associado a eles. Ao usar uma tabela de log, você pode não apenas obter relatórios de desempenho, mas também obter insights com mensagens de depuração durante seu desenvolvimento.

Não se esqueça de podar a tabela de log para manter seu tamanho sob controle. Talvez arquivando-o em algum lugar (talvez por meio de outro evento!).