我需要帮助从包含特定对象(即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故障的极端示例 –