2

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

因此,不是为每种新文档类型创建一个表,而是经过一些研究和努力,我决定使用 EAV 结构来处理文档属性,我知道它们的缺点,但我认为我没有其他方便的方法来实现这一点,我是我吗?!!

无论如何,我使用了 EAV 结构http://eav.codeplex.com/的 codeplax 示例,他们为数据库中的每个 EAV“对象”创建一个视图,它可以作为普通表轻松查询。

我决定走得更远,为视图编制索引,这样我就可以获得性能,问题就来了:

  • 方案:通过

  • 左连接:通过

  • 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
4

1 回答 1

3

First thing you need to do is read Best Practices for Semantic Data Modeling for Performance and Scalability. There are many traps in the path you choose.

As for indexed views, the restrictions are documented at Creating Indexed Views, where MIN and MAX are very explicitly forbidden. The reason why is explained in Why can't indexed views have a MAX() aggregate?

于 2013-03-04T09:59:40.373 回答