Hi,
I am trying to configure a job which does data purges historical data in a table ( data older than 2 months) in chunks. The table has a primary key as well as date column. My plan is to use the date column range to find all the primary key’s and using that delete the rows in chunks
However, I have nor been able to find a substitute in mysql for the oracle PL/SQL countepart. I found something similar in the following link but am not able to get it to work
[MySQL Big DELETEs
For example, for a table with primary key:id and timestamp column:ts
set @a = SELECT MIN(id) FROM table1
LOOP
@z = SELECT id FROM table1 WHERE id >= @a ORDER BY id LIMIT 1000,1
If @z is null
exit LOOP -- last chunk
DELETE FROM table1
WHERE id >= @a
AND id < @z
AND ts < DATE_SUB(CURRENT_DATE(), INTERVAL 60 DAY)
SET @a = @z
sleep 1 – be a nice guy, especially in replication
ENDLOOP
Last chunk:
DELETE FROM tbl
WHERE id >= @a
AND ts < DATE_SUB(CURRENT_DATE(), INTERVAL 30 DAY)