我需要编写一个 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;
最佳答案
您没有设置 numrows,请尝试 select..into numrows。
关于Mysql 存储过程未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50796717/