Logo
You
Code

Kill slow querys MySql

Autor YouCode - http://www.youcode.com.ar/mysql/kill-slow-querys-mysql-295

Vamos a realizar un KILL a los querys lentos.

Realizaremos estos script en la base de datos mysql, el procedimiento almacenado se ejecutara cada 10 segundos por medio de un evento o schedule.

Para matar los querys lentos crearemos el siguiente procedimiento almacenado:
create procedure purge_slow_queries()
deterministic
begin
    declare done boolean default false;
    declare max_time int default coalesce(@max_kill_time, 200);
    declare pid bigint;
    declare c cursor for
    SELECT id
    FROM information_schema.processlist
    WHERE state in ('executing')
        AND time > max_time;
    declare continue handler for not found
        set done = true;
    open c;
    set @q_kill = 'KILL ?';
    prepare q_kill from @q_kill;
    PURGELOOP: loop
        fetch c into pid;
        if done then
            leave PURGELOOP;
        end if;
        set @pid = pid;
        execute q_kill using @pid;
    end loop;
    deallocate prepare q_kill;
end;
Ahora nos falta crear el evento:
create event auto_purge_slow_queries
    on schedule every 10 second
    do call purge_slow_queries();
Es importante que luego de crear el SCHEDULE lo editemos, por ejemplo con el EMS para quitar el tilde en
"on completion not preserve" dado que si esta marcado se borrara al ajecutarse una vez.

Vean tambien http://www.youcode.com.ar/mysql/kill-sleeping-connections-294
http://www.youcode.com.ar/mysql/kill-slow-querys-mysql-295