我正在尝试运行此查询:
delete FROM `customer` where customer_id
not in (SELECT distinct customer_id FROM `order`);
但是需要很长时间,导致超时和中间中断,因为 customer
表有超过一百万条记录。
如何才能快速完成?还有其他选择吗?
编辑
这里是与 SELECT 相同的解释:
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY customer index NULL PRIMARY 4 NULL 127659 Using where; Using index
2 DEPENDENT SUBQUERY order ALL NULL NULL NULL NULL 25141 Using where
最佳答案
尝试:
DELETE customer FROM customer t1 LEFT JOIN `order` t2 on t1.customer_id = t2.customer_id WHERE t2.customer_id is null;
关于mysql - 如何优化这个删除查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27858202/