简体   繁体   中英

rails, optimize db query

I have such models structure:

class Entity < ActiveRecord::Base
  habtm :properties_1
  # ... 
  habtm :properties_N
end

I need to write request by any set of properties, it causes N joins:

Entity.joins(:properties_1, :properties_N).where("properties_1.id = ? AND properties.N = ?", p1_id, pN_id) 

N may be > 20

What can I use to increase query performance?

I think about caching all this settings to big table with serialized attributes, but it looks like this problem is common and, probably, any existed solution is present.

Any ideas?

Thanks for advance!

this is looks like Entity-Attribute-Value (EAV) pattern.

http://www.slideshare.net/stepanyuk/implementation-of-eav-pattern-for-activerecord-models-13263311

https://github.com/kostyantyn/hydra_attribute

EAV is not good solution in relational databases, it is better to use document-oriented DB like MongoDB

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM