具有索引视图的SQL EAV结构

时间:2013-03-04 08:54:31

标签: sql sql-server indexing entity-attribute-value sql-view

我正在开发一个通用文档工作流程系统,每个文档都有不同的属性,但它们共享相同的轮廓并经历相同的工作流程。

因此,不是为每个新的文档类型创建一个表,经过一些研究和斗争,我决定采用EAV结构来获取文档属性,我知道他们的缺点,但我认为我没有其他方便的方法来实现这个,我是我?!!

无论如何,我选择了EAV结构http://eav.codeplex.com/的codeplax样本,他们根据数据库中的每个EAV“对象”创建一个视图,可以很容易地将其作为普通表进行查询。

我决定再进一步索引视图以便我获得性能,这就是问题:

  • SCHEMABINDING:传递

  • 左连接:传递

  • MIN聚合函数:无法通过!

无论如何我可以索引视图或获取索引结果!??

最后一个视图查询是:

        CREATE VIEW [dbo].[vComputer1]
    WITH SCHEMABINDING 
    AS
    SELECT       
O.ObjectID, MIN(O.Name) AS Name, 
MIN(CASE WHEN V.AttributeID = 4 THEN V.Value ELSE NULL END) AS Make, 
MIN(CASE WHEN V.AttributeID = 5 THEN V.Value ELSE NULL END) AS  Model, 
MIN(CASE WHEN V.AttributeID = 6 THEN V.Value ELSE NULL END) AS Type,  
MIN(CASE WHEN V.AttributeID = 7 THEN V.Value ELSE NULL END) AS CPU, 
MIN(CASE WHEN V.AttributeID = 8 THEN V.Value ELSE NULL END) AS Drive,  
MIN(CASE WHEN V.AttributeID = 9 THEN V.Value ELSE NULL END) AS Video, 
MIN(CONVERT(INT, CASE WHEN V.AttributeID = 10 THEN V.Value ELSE NULL END))  AS RAM,
 MIN(CASE WHEN V.AttributeID = 11 THEN V.Value ELSE NULL END) AS Optical,
 MIN(CASE WHEN V.AttributeID = 12 THEN V.Value ELSE NULL END) AS Battery,
 MIN(CASE WHEN V.AttributeID = 13 THEN V.Value ELSE NULL END) AS Screen, 
 MIN(CASE WHEN V.AttributeID = 14 THEN V.Value ELSE NULL END) AS OS,
 MIN(CASE WHEN V.AttributeID = 15 THEN V.Value ELSE NULL END) AS PurchaseDate
    FROM            dbo.Object AS O INNER JOIN
                             dbo.Value AS V ON ISNULL(V.ObjectID, 1) = O.ObjectID INNER JOIN
                             dbo.Category AS C ON ISNULL(C.CategoryID, 2) = O.CategoryID
    WHERE        (C.Name = 'Computer')
    GROUP BY O.ObjectID

1 个答案:

答案 0 :(得分:3)

您需要做的第一件事是阅读Best Practices for Semantic Data Modeling for Performance and Scalability。您选择的路径中有多个陷阱。

对于索引视图,限制记录在Creating Indexed Views,其中MINMAX被明确禁止。原因在Why can't indexed views have a MAX() aggregate?

中解释