[英]How can I restructure this large PostgreSQL 9.3 table to be indexed and searched more efficiently from a Rails application?
我在正在處理的該應用程序中引入了設計缺陷。 問題域涉及將Product
與本質上是每個Customer
50個可能屬性的稀疏矩陣進行匹配。 有數百萬行。 因此,重申一下:
Product has_many ProductAttributes
Customer has_many ProductAttributes
每個Customer
Product
都有一組不同的ProductAttributes
。
因此,我有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個記錄。 我可以在value列中搜索ProductAttribute
,返回CustomerProdct
父記錄。
所以三種可能的方法:
HStore
JSON
Detail table
感謝您提供的任何見解。
您可以絕對查詢Hstore,我認為這可能是一個很好的解決方案,因為在您的Schema設計中不使用更高的標准格式,如果您不是數據庫人,這可能很難。
您可以執行以下查詢:
Product.where("attributes -> 'Color' = 'Blue'")
Product.where("attributes -> 'Size' LIKE '%L%'") #finds 'Large' and 'Long' for ex.
查看此rails cast ,完整披露,其pro cast(成本$)
但是,請考慮嘗試使您的Schema進入3NF,以便以更加數據庫化的方式解決它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.