Created
February 11, 2015 03:28
-
-
Save jheth/442ee65d9c58e0beaf71 to your computer and use it in GitHub Desktop.
Clear Old Session Records
This file contains 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
DROP procedure IF EXISTS `ClearSessions`; | |
DELIMITER $$ | |
CREATE PROCEDURE ClearSessions(IN days_ago INT) | |
BEGIN | |
DECLARE bDone INT; | |
DECLARE count INT; | |
DECLARE sessionId INT; | |
DECLARE curs CURSOR FOR SELECT id FROM sessions WHERE updated_at < DATE_SUB(NOW(), INTERVAL days_ago DAY); | |
DECLARE CONTINUE HANDLER FOR NOT FOUND SET bDone = 1; | |
OPEN curs; | |
SET bDone = 0; | |
SET count = 0; | |
REPEAT | |
FETCH curs INTO sessionId; | |
DELETE FROM sessions WHERE id = sessionId; | |
SET count = count + 1; | |
UNTIL bDone END REPEAT; | |
CLOSE curs; | |
SELECT count AS 'Deleted'; | |
END$$ | |
DELIMITER ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment