我需要幫助從包含特定對象(即Products)和其動態屬性的一個表的模型進行查詢。幫助查詢SQL Server 2012中的實體屬性值模型(「動態屬性」)
比方說,我可以一個Chocolate
存儲與屬性,如Price
,Color
,Weight
也有屬性,如Engine
,Gears
,Color
一個Car
。
在我的例子,我有一個叫做產品具有以下幾列表:
Id (Int),
Name (NVarchar)
我有另一個表稱爲dynamicAttributes
有以下欄目:
Id (int) -- of the attribute
ProductId (int) -- of the specific product
AttributeType (int) -- enum with the following values ("Color", "Price","Height", "Width"....)
StringValue -- of the product
IntValue -- of the product
DoubleValue -- of the product
BooleanValue -- of the product
我從客戶端列表中獲取屬性代碼及其值的列表。
我可以爲每個屬性獲取值類型(aka布爾值,字符串,int)。
從我的應用程序中查詢此模型的最佳選擇是什麼?
僅限動態sqls?使用Pivot關鍵字?
這種設計反模式的名稱是「實體att肋骨值「。搜索它。有很多關於這方面的帖子。 –
真的很糟糕的想法 - 你應該儘量避免使用EAV - 它們看起來像是一個明智的選擇,但是隨着時間的推移,它們將成爲一個巨大的性能和維護噩夢 - 更不用說它需要的非常低效和雜亂的查詢從中獲取任何數據。 .... **不要這樣做!** –
請參閱[避免EAV的破壞(Joe Celko)](http://www.simple-talk.com/sql/t-sql-programming/avoiding-the-eav-of-destruction/),[你應該避免的五個簡單的數據庫設計錯誤(點#3)](http://www.simple-talk.com/sql/database-administration/five-simple--database-design-errors-you-should-avoid/ )和[Bad CarMa](http://www.simple-talk.com/content/article.aspx?article=292)作爲大規模EAV故障的極端示例 –