使用MySQL删除块

时间:2018-06-05 20:32:42

标签: mysql sql-delete

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

错误:

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;

代码:

{{1}}

1 个答案:

答案 0 :(得分:0)

Sorry, something went wrong. You may want to try again.BEGIN ... END语句仅在MySQL存储程序的上下文中有效(例如LOOPPROCEDUREFUNCTION,< / p>

作为使用示例:

TRIGGER

鉴于我们没有看到DELIMITER $$ CREATE PROCEDURE foo BEGIN SET ... ; SET ... ; myloop: LOOP ... END LOOP myloop; END$$ DELIMITER ; 语句,我们将猜测错误消息是将其作为裸语句执行的结果。语法错误是预期的结果。

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

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