查询执行时间太长

时间:2016-11-25 03:48:26

标签: mysql

我已经编写了一个sql查询,但是花了太多时间从table获取结果。我的表中有15000个条目。我的查询和表结构如下。

SELECT sp1.sku
FROM `service_prices` AS sp1
WHERE attribute_id = 8 AND
      attribute_value = '100000' AND
      service_id = 2 AND
      sp1.sku IN
      (
          SELECT sp2.sku
          FROM `service_prices` AS sp2
          WHERE attribute_id = 9 AND
                attribute_value = '2' AND
                service_id = 2 AND
                sp2.sku IN
                (
                    SELECT sp3.sku
                    FROM `service_prices` AS sp3
                    WHERE attribute_id = 10 AND
                          attribute_value = 'Haryana' AND
                          service_id = 2
                )
            )

enter image description here

任何帮助都会感激不尽。

1 个答案:

答案 0 :(得分:2)

SELECT
  sp1.sku
 FROM
`service_prices` AS sp1
 INNER JOIN `service_prices` AS sp2 ON sp1.sku= sp2.sku INNER JOIN `service_prices` AS sp3 ON  sp2.sku= sp3.sku
WHERE
sp1.attribute_id = 8
AND sp1.attribute_value = '100000'
AND sp1 service_id = 2 and
sp2.attribute_id = 9
        AND sp2.attribute_value = '2'
        AND sp2.service_id = 2
and 
sp3.attribute_id = 10
                AND sp3.attribute_value = 'Haryana'
                AND sp3.service_id = 2