Written By ROMAN AGABEKOV | MAY 10, 2024
Last update | MAR 7, 2025
CREATE EVENT kill_long_running_queries
ON SCHEDULE EVERY 1 MINUTE -- Specifies how often the event runs
DO
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE proc_id INT; -- Variable to store the process ID of each query
DECLARE cur1 CURSOR FOR SELECT ID FROM information_schema.processlist
WHERE Command = 'Query' AND Time > 60; -- Change '60' to your threshold in seconds
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO proc_id;
IF done THEN
LEAVE read_loop;
END IF;
KILL proc_id; -- Kills the process identified by proc_id
END LOOP;
CLOSE cur1;
END;