Created
November 26, 2013 14:49
-
-
Save lxfontes/7659580 to your computer and use it in GitHub Desktop.
kill idle mysql sessions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| DELIMITER ;; | |
| CREATE DEFINER=`root`@`localhost` PROCEDURE `uKillSleepingSessions`() | |
| READS SQL DATA | |
| COMMENT 'This routne is used to kill idle sessions' | |
| BEGIN | |
| DECLARE no_more_rows BOOLEAN; | |
| DECLARE loop_cntr INT DEFAULT 0; | |
| DECLARE num_rows INT DEFAULT 0; | |
| DECLARE uID bigint(4); | |
| DECLARE my_cur CURSOR FOR | |
| SELECT ID | |
| FROM information_schema.PROCESSLIST PL | |
| WHERE PL.COMMAND='Sleep' AND PL.TIME > 180; | |
| DECLARE CONTINUE HANDLER FOR NOT FOUND | |
| SET no_more_rows = TRUE; | |
| OPEN my_cur; | |
| select FOUND_ROWS() into num_rows; | |
| the_loop: LOOP | |
| FETCH my_cur | |
| INTO uID; | |
| IF no_more_rows THEN | |
| CLOSE my_cur; | |
| LEAVE the_loop; | |
| END IF; | |
| SET @tmp_sql= CONCAT("KILL ",uID); | |
| PREPARE s1 FROM @tmp_sql; | |
| EXECUTE s1; | |
| DEALLOCATE PREPARE s1; | |
| SET loop_cntr = loop_cntr + 1; | |
| END LOOP the_loop; | |
| END;; | |
| DELIMITER ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment