简体   繁体   English

Rails,优化数据库查询

[英]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: 我需要通过任何一组属性编写请求,这会导致N个连接:

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

N may be > 20 N可能> 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. 这看起来像Entity-Attribute-Value(EAV)模式。

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

https://github.com/kostyantyn/hydra_attribute https://github.com/kostyantyn/hydra_attribute

EAV is not good solution in relational databases, it is better to use document-oriented DB like MongoDB EAV在关系数据库中不是一个好的解决方案,最好使用像MongoDB这样的面向文档的数据库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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