MySql和subselect,为什么这么慢?

MySql和subselect,为什么这么慢?,mysql,query-optimization,subquery,Mysql,Query Optimization,Subquery,我在这里选择: select parent_id from sales_flat_order_status_history where status like '%whatever%' group by parent_id having count(parent_id) > 1 此查询只运行几秒钟。现在我想在另一个select中使用它,如下所示: select increment_id from sales_flat_order where entity_id in( select pa

我在这里选择:

select parent_id from sales_flat_order_status_history where status like '%whatever%' group by parent_id having count(parent_id) > 1
此查询只运行几秒钟。现在我想在另一个select中使用它,如下所示:

select increment_id from sales_flat_order where entity_id in(
select parent_id from sales_flat_order_status_history where status like '%whatever%' group by parent_id having count(parent_id) > 1)
这将永远运行,因此我尝试逐个插入ID:

select increment_id from sales_flat_order where entity_id in(329,523,756,761,763,984,1126,1400,1472,2593,3175,3594,3937,...)
这运行得很快,区别在哪里?我如何使我的第一种方法运行得更快


谢谢

子查询以foreach样式处理(对于每一行,执行subselect)


类似于(1,2,3)中的
不要对一些旧版本的mysql使用索引。

子查询以foreach样式处理(对于每一行,请执行subselect)


类似于(1,2,3)中的
不要对一些较旧版本的mysql使用索引。

您的查询需要很长时间才能运行,因为它执行子查询并查找每行sales\u flat\u order表

加入可能会更快:

select increment_id 
from sales_flat_order
  inner join (select parent_id 
              from sales_flat_order_status_history 
              where status like '%whatever%' 
              group by parent_id having count(parent_id) > 1) Sub 
  on sales_flat_order.entity_id  = Sub.parent_ID

这将强制子查询只执行一次

您的查询需要很长时间才能运行,因为它正在执行子查询并查找每一行sales\u flat\u order表

加入可能会更快:

select increment_id 
from sales_flat_order
  inner join (select parent_id 
              from sales_flat_order_status_history 
              where status like '%whatever%' 
              group by parent_id having count(parent_id) > 1) Sub 
  on sales_flat_order.entity_id  = Sub.parent_ID

这将强制子查询只执行一次

,因此我无法使其更快?因此我无法使其更快?是的,确实快得多!谢谢,真的快多了!谢谢