我试图运行此查询:如何优化这个删除查询?
delete FROM `customer` where customer_id
not in (SELECT distinct customer_id FROM `order`);
但它需要很长的时间,导致中间timout和休息,因为customer
表中有超过一百万条记录。
我怎样才能让它快速?任何选择?
编辑
这里是相同的EXPLAIN与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
'customer.customer_id'和'order.customer_id'编入索引吗? – h2ooooooo
是的,你的索引状态的一些想法会有所帮助,但它可能只是你的环境无法处理它。在SQL方面,根据您的硬件和索引策略,一百万行可能几乎无足轻重,或者可能是一项不可能实现的重大任务。 –
这个问题应该可能在http://codereview.stackexchange.com/ – Jave