У меня есть требование написать SP, где - 1. Выберите количество строк, соответствующее заданному предложению where.2. Зацикливайтесь на счетчиках строк и выполняйте запрос на удаление с пределом n, пока счетчик строк времени не станет равным 0. 3. Уменьшите значение счетчика строк на n.
SP записано -
BEGIN
DECLARE current_timestamp_millis BIGINT;
DECLARE RETENTION_DAYS SMALLINT;
DECLARE numRows BIGINT DEFAULT 0;
-- No. of days to retain data for
SET RETENTION_DAYS = 1;
-- Current epoch timestamp in millis
SET current_timestamp_millis = UNIX_TIMESTAMP(NOW())*1000;
-- SQL query to get the count of rows ,eligible to get deleted.
select count(*) as numRows from table1 where state = 2 AND end_time < (UNIX_TIMESTAMP(NOW())*1000 - (1 * 24 * 60 * 60 * 1000)) ;
-- Loop on the num of rows from above select query and delete the rows in chunks of 100
WHILE(numRows >=0)
DO
Insert into test_t values(current_time_millis);
Delete from table1 where end_time < ((UNIX_TIMESTAMP(NOW())*1000 - (1 * 24 * 60 * 60 * 1000))) limit 100;
SET numRows = numRows - 100;
DO SLEEP(2);
END WHILE;
END;