I’m grabbing the last price from a specific product, but performance is really slow on over 3 million prices.
Does anyone have a better way of doing this? My server is getting hammered by this slow query.
prices.id is used to store the id of the store in the prices table so I can join it with the stores.id from the stores table.
SELECT prices.id, prices.price, prices.timelog, prices.user_id FROM prices
WHERE prices.id IN (SELECT stores.id FROM stores WHERE city = "miami" )
AND prices.product_id = 1
AND prices.timelog IN
(SELECT MAX( lastprice.timelog )
FROM prices AS lastprice
WHERE lastprice.id = prices.id AND lastprice.product_id = 1)
3 million prices might mean your application could benefit from some denormalisation. (i.e. flag the last price in some manner, remember: write-once, read-many is often worth the overhead of a slower write).
With the current data, worth a try are:
… and another a bit faster option which always escapes me this time of night 🙂
Keep in mind a lot can change with the proper indexes on tables, so by all means, run an
EXPLAINon the lot.