MYSQL批量删除80个表

fat*_*zzy 5 mysql dependencies transactions

我有50GB的mysql数据库(80个表),我需要从中删除一些内容.我有一个参考表,其中包含需要从其他表中删除的产品ID的列表.

现在,其他表各为2 GB,包含需要删除的项.

  1. 我的问题是:由于它不是一个小型数据库,为了避免出现问题,一次删除数据最安全的方法是什么?

  2. 验证整个数据被删除的最佳方法是什么?

bur*_*all 0

我同意 Thirler 的观点,即最好使用外键。它保证了整个数据库的引用完整性和一致性。
我相信生活有时需要更棘手的逻辑。所以你可以使用手动查询,例如

delete from a where id in (select id from keys)  
Run Code Online (Sandbox Code Playgroud)

您可以一次删除所有记录,也可以按键范围或在 DELETE 中使用 LIMIT 来删除所有记录。适当的索引是必须的。要验证一致性,您需要函数或查询。例如:

create function check_consistency() returns boolean
begin
   return not exists(select * from child where id not in (select id from parent) ) 
       and not exists(select * from child2 where id not in (select id from parent) );
   -- and so on  
end
Run Code Online (Sandbox Code Playgroud)