【问题标题】:How can I speed up the execution of this query (subselects)?如何加快此查询(子选择)的执行速度?
【发布时间】:2014-05-29 09:03:31
【问题描述】:

这是我的查询,它的运行速度非常慢。我怎样才能提高性能?也许使用连接而不是 sunselects?

select 
sfoa.firstname, 
sfoa.lastname, 
sfo.increment_id 
from sales_flat_order_address sfoa 
join sales_flat_order sfo 
on sfoa.parent_id = sfo.entity_id 
where parent_id 
in 
(
SELECT
order_id
FROM 
(
SELECT
  sfoi.order_id,
  GROUP_CONCAT(sfoi.sku SEPARATOR ', ') AS skus
  FROM sales_flat_order_item sfoi
  GROUP BY sfoi.order_id) t
  WHERE t.skus LIKE '%whatever%'
  AND t.skus RLIKE '[0-9]'
);

感谢您的帮助!

【问题讨论】:

标签: mysql performance


【解决方案1】:

是的,IN 带有子查询很慢。请改用join

每次评估子查询时都会执行(无论如何在 MySQL 中,不是所有的 RDBMS),也就是说,您基本上是在运行 700 万个查询!如果可能,使用 JOIN 会将其减少到 1。即使添加索引可以提高它们的性能,您仍在运行它们。

https://dev.mysql.com/doc/refman/5.6/en/subquery-optimization.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-17
    • 2014-10-04
    • 1970-01-01
    相关资源
    最近更新 更多