0

I run a Rails app with postgresql as a database. I do a lot of joined searches and the queries tend to be quite slow. I don't quite understand if it just the nature of joined searches that makes the queries slow or if I have failed to make an accurate index for them.

Example (my queries are usually more complex but this shows what I mean):

Product belongs_to :store
Store has_many :products
Store.is_open :boolean
Product.has_image :boolean

I make queries like:

Store.where(:is_open => true).products.where(:has_image => true)

Do I:

  1. Make indexes only Store and Product exclusively like:

    add_index :stores, :is_open add_index :products, :has_image

This is what I do now.

OR

  1. Can I make a joined index that includes both tables?
Christoffer
  • 2,271
  • 3
  • 26
  • 57
  • Related reading: http://stackoverflow.com/a/26251892/525478 – Brad Werth Aug 24 '15 at 08:16
  • Short answer: "no". Possible duplicate of http://stackoverflow.com/questions/16402225/index-spanning-multiple-tables-in-postgresql – Max Williams Aug 24 '15 at 08:52
  • Thanks, you can put that as an answer if you want. I have to make it speedier in other ways, I guess. I have just started experimenting with bullet gem. – Christoffer Aug 24 '15 at 09:41

0 Answers0