有没有办法加快此 MySQL 删除查询的速度?

Is there a way to speed up this MySQL delete query?

我正在使用 MySql 5.5.37。我正在尝试找出一种更快的方法来编写以下查询。我有两个 table,access_code 和教室。 “教室”table 对 access_code table 的主键有一个外键约束(access_code_id)。我正在尝试删除 access_code table 中没有 link 到教室 table 并且具有特定类型 ID 的记录。所以我 运行 …

delete from access_code where id in (select q.* from (select a.id FROM access_code a left join lyc_classroom c on a.id = c.access_code_id where a.access_code_type = 2 and c.access_code_id is null) q);

这需要很长时间。有没有更快的方法来执行上述查询?

您不需要子查询。使用 delete ... join 语法

delete a 
from access_code a
left join lyc_classroom c on a.id = c.access_code_id
where a.access_code_type = 2 and c.access_code_id is null