I’m trying to optimize this query, and as simple as it sounds, I’m having a hard time figuring out how to speed it up.
I have two objects, a Widget and a Category (HABTM relationship), with the tables “widgets”, “categories”, and “categories_widgets” (join table). I’m trying to find all the widgets in a particular category. Simple right? Some categories can have 10-50k widgets though, and my queries are taking 1-2 seconds to complete (my site is fairly high traffic, so this is completely unacceptable).
Here’s what I’m doing now:
SELECT * FROM `categories`
LEFT JOIN `categories_widgets` ON `categories_widgets`.`category_id`=`categories`.`id`
JOIN `widgets` ON `widgets`.`id`=`categories_widgets`.`widget_id`
ORDER BY `widgets`.`id` DESC
LIMIT 0,10
I do need to order the widgets by id descending (this is what kills the speed), and I can sometimes have decently high offsets (not sure if anything can be done about that).
Any help with this would be appreciated, or alternatively if you have any recommendations for table restructuring, that’s a possibility as well (but assuming a lot of categories and a lot of widgets with no easy way to shard).
Thank you!
The table layout seems right.
Why the
LEFT JOIN? I made that aJOIN.Where is your
WHEREclause? “find all the widgets in a particular category.”And you do have indexes on
categories_widgets(category_id)andwidgets(id), right?