CREATE PROCEDURE prc_clean_tables (Y INT, Z INT)
BEGIN
BEGIN TRANSACTION;
DECLARE _now DATETIME;
SET _now := NOW();
INSERT
INTO b
SELECT *
FROM a
WHERE timestamp < _now - INTERVAL Y DAY;
FOR UPDATE;
DELETE
FROM a
WHERE timestamp < _now - INTERVAL Y DAY;
DELETE
FROM b
WHERE timestamp < _now - INTERVAL Z DAY;
COMMIT;
END