如何限制MySQL中的执行时间?

时间:2015-10-17 10:15:06

标签: mysql stored-procedures execution-time

我有以下代码来限制MySQL中的执行时间,但它无法正常工作。 有人帮助我。

delimiter //
   CREATE PROCEDURE `classicWatches`.kill_long_running_queries()

BEGIN
  DECLARE process_id BIGINT;
  DECLARE finished INT DEFAULT 0;
  DECLARE kill_process_id CURSOR FOR SELECT ID FROM INFORMATION_SCHEMA.PROCESSLIST WHERE COMMAND = 'Query' AND TIME > 5; # only for 5 seconds just testing whether it works or not
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;
  OPEN kill_process_query;

  loop_loop: LOOP

FETCH kill_process_id INTO process_id;   

IF process_id >=1 THEN 
    KILL QUERY process_id ; # Here showing error in mysql workbench but don't know what error
END IF ;
IF finished THEN

  LEAVE loop_loop;
END IF;
  END LOOP loop_loop;
     CLOSE kill_process_id;

END //
delimiter ;

CREATE EVENT kill_long_running_queries
ON SCHEDULE EVERY 10 SECOND
DO CALL `classicWatches`.kill_long_running_queries();

1 个答案:

答案 0 :(得分:0)

我不太确定你想要完成什么。

enter image description here Workbench中显示的错误是因为需要一个整数(如第23行所示 - 第23行只是一个示例),但是找到了process_id变量,但是,在调用时会创建并正确执行存储过程。

enter image description here 光标为kill_process_id,但在第13行打开光标kill_process_query不存在,您需要更改第14行。