解决方法
无论MS sql Server与任何其他品牌的数据库无关,EAV最糟糕的性能问题是人们尝试使用怪物查询重建单个行上的实体.这需要每个属性单独连接.
SELECT e.id,a1.attr_value as "cost",a2.attr_value as "color",a3.attr_value as "size",. . . FROM entity e LEFT OUTER JOIN attrib a1 ON (e.entity_id = a1.entity_id AND a1.attr_name = 'cost') LEFT OUTER JOIN attrib a2 ON (e.entity_id = a2.entity_id AND a2.attr_name = 'color') LEFT OUTER JOIN attrib a2 ON (e.entity_id = a3.entity_id AND a3.attr_name = 'size') . . . additional joins for each attribute . . .
无论您使用什么数据库品牌,查询中的多个连接都意味着几何增加的性能成本.不可避免地,您需要足够的属性来超越任何sql引擎的架构容量.
解决方案是以行而不是列取出属性,并在应用程序代码中编写一个类,以循环遍历这些行,将值分配给对象属性.
SELECT e.id,a.attr_name,a.attr_value FROM entity e JOIN attrib a USING (entity_id) ORDER BY e.id;