我需要写一个sp where-1。选择与给定where子句匹配的行数。2循环行计数并执行限制为n的delete查询,直到时间行计数不为0。三。将行计数值减少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;
1条答案
按热度按时间zsohkypk1#
您没有设置numrows try select..into numrows。