0

This is my query, its running terribly slow. How can I improve the performance? Maybe using a join instead 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]'
);

Thanks for helping!

jmail
  • 5,944
  • 3
  • 21
  • 35
user1856596
  • 7,027
  • 10
  • 41
  • 63

1 Answers1

0

Yes, IN with subqueries is slow. Use a join instead.

Subqueries execute every time you evaluate them (in MySQL anyway, not all RDBMSes), i.e. you're basically running 7 million queries! Using a JOIN, if possible, will reduce this to 1. Even if adding indexing improves performance of those, you're still running them.

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

jmail
  • 5,944
  • 3
  • 21
  • 35