mysql - 使用 MySQL 分块删除

标签 mysql sql-delete

我正在尝试从我的 AWS RDS MySQL 实例中分块删除大量数据。我正在尝试调整此 link 中找到的代码,但我遇到了无法修复的语法错误。

错误:

SQL Error (1064): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'main1: LOOP
    SELECT @z := operation_id FROM operations_test WHERE operation_id' at line 2

代码:

SET @apt = 'DTW'; 

SET @a = (SELECT MIN(operation_id) FROM operations_test);

BEGIN
    main1: LOOP
        SELECT @z := operation_id FROM operations_test WHERE operation_id >= @a ORDER BY operation_id LIMIT 1000,1;
        IF @z IS NULL THEN
          LEAVE main1;  -- last chunk
       END IF;
       DELETE operations_test, profiles_test FROM profiles_test LEFT JOIN operations_test ON operations_test.operation_id=profiles_test.operation_id WHERE operations_test.airport_id = @apt
            AND operations_test.operation_id >= @a
             AND operations_test.operation_id <  @z;
       DELETE operations_test FROM operations_test WHERE airport_id = @apt
            AND operation_id >= @a
             AND operation_id <  @z;
       SET @a = @z;
        SLEEP 1;  -- be a nice guy, especially in replication
    END LOOP main1;
END;
# Last chunk:
DELETE operations_test, profiles_test FROM profiles_test LEFT JOIN operations_test ON operations_test.operation_id=profiles_test.operation_id WHERE operations_test.airport_id = @apt
    AND id >= @a;
DELETE operations_test FROM operations_test WHERE airport_id = @apt
    AND id >= @a;

最佳答案

BEGIN ... ENDLOOP 语句仅在 MySQL 存储程序的上下文中有效(例如 PROCEDURE功能触发,)

作为使用示例:

DELIMITER $$

CREATE PROCEDURE foo 
BEGIN 
   SET ... ;
   SET ... ; 
   myloop: LOOP 
     ...
   END LOOP myloop;
END$$

DELIMITER ;

鉴于我们没有看到 CREATE PROCEDURE 语句,我们将猜测错误消息是将其作为裸语句执行的结果。语法错误是预期的结果。

https://dev.mysql.com/doc/refman/5.7/en/begin-end.html

https://dev.mysql.com/doc/refman/5.7/en/loop.html

关于mysql - 使用 MySQL 分块删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50708512/

相关文章:

php - MySQL查询显示问题

mysql - 如何在 MYSQL 中创建仅包含 DELETE 查询的过程?

mysql - 如果时间超过 4 小时,则从数据库中删除

mysql 哪里计数(column_name)= 1?

mysql - 插入新行或删除旧行(如果已存在)

ASP.NET从bll插入到mysql

mysql - 如何使用条件连接搜索两个 MySql 表

php - 如何限制 MySQL Select 查询结果截止日期

MYSQL,这种请求可以吗?

mysql - "delete from table where NULL = NULL"是什么意思?