Akhil Akhil - 13 days ago 5
MySQL Question

Query Taking too long to execute

I have written an sql query but its taking too much time to get results from table.Around 15000 entries are there in my table.My query and table structure is below..

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

Any help would be grateful.

Answer
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