Mysql – optimizing a select query of 1.5 million entry

MySQLoptimizationperformancequery-performance

I have the following query:

SELECT s0_.id, s0_.lowresimageurl
FROM app_instagram_shop_picture s0_
FORCE INDEX ( idx_deleted_shopId )
LEFT JOIN app_instagram_shop s1_ ON s0_.shop_id = s1_.id
AND (
s1_.deletedAt IS NULL
)
WHERE (
s1_.expirydate IS NULL
)
AND (
s0_.deletedAt IS NULL
)
ORDER BY s0_.checked ASC
LIMIT 0 , 30

the table of app_instagram_shop_picture has 1.5 million entry and the query above takes about 10 seconds to finish. Here's the structure and indexes that is in that table:

http://cl.ly/image/3E3T2B001W0Q

Here's the structure and indexes of the app_instagram_shop table.

I am running out of ideas on how I can further optimize this again.

Best Answer

Would that work?

SELECT s0_.id, s0_.lowresimageurl
FROM app_instagram_shop_picture s0_
FORCE INDEX ( idx_deleted_shopId )
WHERE EXISTS (SELECT 1 FROM app_instagram_shop s1_
            WHERE s0_.shop_id = s1_.id
                AND s1_.deletedAt IS NULL
                AND s1_.expirydate IS NULL
                AND s0_.deletedAt IS NULL)
ORDER BY s0_.checked ASC
LIMIT 0 , 30