Hi I tested to remove the .order(...) part and indeed, the query time goes down to ~100ms. However, it doesn't help to add indices, at least not as I did :)
add_index :categories, :name add_index :subcategories, :name Did some more testing and if I keep the .order... but don't join the products table I get a query that runs at about ~55ms. So the bottleneck seems to be the products table. The query that I'm running looks like this: Category.eager_load(:subcategories) .joins("INNER JOIN products AS p ON resellercategories.id = p.resellercategory_id") .order("categories.name ASC, subcategories.name ASC") (Skipping the gender here...) What I have is Categories and Subcategories. They are related to each other through a Resellercategories table. Products are related to Resellercategories. So, the reason that I want to join the products as well is because I only want to show categories and subcategories that actually have some products (there are some empty categories/subcategories still). So the above query is what we came up with in another thread here in the group. - Maybe there is a better way to check if a category/subcategory has products without joining the entire products table? BTW, in my dev database there is about 8700 rows in products table, 2200 rows in resellercategories, 5 rows in categories and 45 rows in subcategories. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/1AqHbcGn9n8J. To post to this group, send email to rubyonrails-talk@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.