我在我正在研究的这个应用程序中引入了一个设计缺陷。问题域涉及将Product
与基本上是每Customer
个50个可能属性的稀疏矩阵相匹配。有数百万行。所以要重申:
Product has_many ProductAttributes
Customer has_many ProductAttributes
每个Product
ProductAttributes
会有Customer
的不同{。}}。
所以我有这个5000万行的表,而且绝大多数字段都是空的。看起来很痛苦。以下是我的一些问题。
citext
。我更愿意使用HStore或JSON字段重做整个情况。但我担心我能够对其中任何一个进行搜索。可以针对其中任何一个进行全文搜索吗? HStore键值对会限制我吗?目前,每个ProductAttribute
只能有一个值,但我可以设想一种情况,我可以在其中至少使用其中一个值的数组。虽然不是交易杀手。
如果我可以搜索JSON和/或HStore,考虑到我在Rails 3.2应用程序中使用ActiveRecord,哪个是最好的?
另一种选择是将ProductAttributes
分解为detail
表:
Product has_many CustomerProducts
Customer has_many CustomerProducts
CustomerProducts has_many ProductAttributes
因此,如果某个产品的特定Customer
只有3个或4个属性,则ProductAttributes
中会有3个或4个记录。我可以只搜索ProductAttribute
的值列,返回CustomerProdct
父记录。
所以有三种可能的方法:
HStore
JSON
Detail table
感谢您提供的任何见解。
答案 0 :(得分:1)
你绝对可以查询Hstore,我认为这可能是一个很好的解决方案,而不是在你的Schema设计中使用更高级的正常形式,如果你不是数据库人员,这可能很难。
您可以执行以下查询:
Product.where("attributes -> 'Color' = 'Blue'")
Product.where("attributes -> 'Size' LIKE '%L%'") #finds 'Large' and 'Long' for ex.
查看此rails cast,完整披露,其专业演员(费用$)
但是,请考虑尝试让Schema进入3NF,以便以更加数据库的方式解决它。