Aurora mysql delete in chunk
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
--- Simple table to keep track of delete chunks | |
create table _delete_log | |
( | |
log varchar(200) null, | |
createdAt timestamp null | |
); | |
------------ Stored proc definition --------------- | |
DROP PROCEDURE IF EXISTS delete_chunks; | |
DELIMITER $$ | |
CREATE PROCEDURE delete_chunks() | |
BEGIN | |
REPEAT | |
DO SLEEP(1); ## Optional, to minimise contention | |
DELETE | |
FROM <TABLE_NAME> | |
WHERE <WHERE CLAUSE> | |
ORDER BY <GOOD TO ORDER THE DELETES> | |
LIMIT 1000; ## Per chunk limit | |
insert into _delete_log SELECT CONCAT(ROW_COUNT(), ' rows deleted'), now(); ## Optional - easy way to track how many deleted so far | |
SHOW ERRORS; | |
UNTIL ROW_COUNT() = 0 END REPEAT; | |
END$$ | |
DELIMITER ; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment