Mysql

Mysql中無休止的查詢循環

  • August 1, 2019

我將從 mysql dbms 中的一張表中刪除數百萬條記錄,但我不想一次刪除所有記錄而不是強制數據庫伺服器 cpu。這就是為什麼我想多次使用我的查詢。我的循環不起作用。我不知道為什麼。怎麼做?我的查詢如下:

while 1=1 DO
delete  FROM customer.generallogs  where DateEnter < '2019-04-03 16:13:28' limit 100000;
end while;

SP 可以像(程式碼草案):

CREATE PROCEDURE DeleteFromGeneralLogs (IN DeleteTill DATETIME)
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done:=1;
-- IF DeleteTill IS NULL THEN LEAVE;
WHILE done = 0 DO
   SELECT SLEEP(1) INTO done; -- small pause between chunks
   DELETE 
       FROM customer.generallogs  
       WHERE DateEnter < DeleteTill  
       LIMIT 100000;
END WHILE;
END;

進而

CALL DeleteFromGeneralLogs('2019-04-03 16:13:28');

引用自:https://dba.stackexchange.com/questions/244326