SQL查看外部应用速度

时间:2013-06-25 15:23:20

标签: sql-server

当使用带有外部应用的视图时,我看到一些奇怪的查询速度结果,我在视图中的2个不同列上进行明显计数,1在不到0.1秒内完成,另一个需要4-6秒,第二个计数查询返回较慢,因为它是外部应用的一部分?如果是这样 - 我怎么能加快这个查询?

快速不同的计数是 -

SELECT DISTINCT ISNULL([ItemType], 'N/A') AS Items FROM vwCustomerItemDetailsFull

缓慢的不同计数是 -

SELECT DISTINCT ISNULL([CustomerName], 'N/A') AS Items FROM vwCustomerItemDetailsFull

视图是 -

SELECT I.ItemID,
       IT.Name AS ItemType,
       CASE
         WHEN CustomerItemEndDate IS NULL
               OR CustomerItemEndDate > GETDATE() THEN CustomerItems.CustomerName
         ELSE NULL
       END     AS CustomerName,
       CASE
         WHEN CustomerItemEndDate IS NULL
               OR CustomerItemEndDate > GETDATE() THEN CustomerItems.CustomerNumber
         ELSE NULL
       END     AS CustomerNumber,
       CASE
         WHEN CustomerItemEndDate IS NULL
               OR CustomerItemEndDate > GETDATE() THEN CustomerItems.CustomerItemStartDate
         ELSE NULL
       END     AS CustomerItemStartDate,
FROM   tblItems I
       INNER JOIN tblItemTypes IT
         ON I.ItemTypeID = IT.ItemTypeID
       OUTER APPLY (SELECT TOP 1 CustomerName,
                                 CustomerNumber,
                                 StartDate AS CustomerItemStartDate,
                                 EndDate   AS CustomerItemEndDate
                    FROM   tblCustomerItems CI
                           INNER JOIN tblCustomers C
                             ON C.CustomerID = CI.CustomerID
                    WHERE  CI.ItemID = I.ItemID
                    ORDER  BY EndDate DESC) AS CustomerItems 

1 个答案:

答案 0 :(得分:3)

检查执行计划,这个速度差异根本不奇怪,因为它是外部申请而不是交叉申请,并且在其中你将结果限制在前1,这意味着你的外部申请没有影响关于查询结果的数量,或列ItemType

因此,当您从视图中选择并且不使用外部应用中的任何列时,优化器足够聪明,知道它不需要执行它。因此,您的首要问题是:

SELECT  DISTINCT ISNULL([ItemType], 'N/A') AS Items
FROM    (   SELECT  tblItems
            FROM    Items
                    INNER JOIN tblItemTypes IT 
                        ON I.ItemTypeID = IT.ItemTypeID
        ) vw

而你的第二个查询必须执行外部申请。

我之前发布的longer answer也很有帮助。

修改

如果您想将查询更改为JOIN,可以将其重写为:

SELECT I.ItemID,
        IT.Name AS ItemType,
        CustomerName,
        CustomerNumber,
        CustomerItemStartDate,
FROM   tblItems I
        INNER JOIN tblItemTypes IT
            ON I.ItemTypeID = IT.ItemTypeID
        LEFT JOIN
        (   SELECT  ci.ItemID,
                    CustomerName,
                    CustomerNumber,
                    StartDate AS CustomerItemStartDate,
                    EndDate   AS CustomerItemEndDate,
                    RN = ROW_NUMBER() OVER (PARTITION BY ci.ItemID ORDER BY EndDate DESC)
            FROM    tblCustomerItems CI
                    INNER JOIN tblCustomers C
                        ON C.CustomerID = CI.CustomerID
        ) AS CustomerItems 
            ON CustomerItems.ItemID = I.ItemID
            AND CustomerItems.rn = 1
            AND CustomerItems.CustomerItemEndDate  < GETDATE();

但是我认为这不会提高性能,因为您说最昂贵的部分是EndDate上的排序,对于您的第一个查询,它会对性能产生负面影响,因为优化器将不再优化外部适用。

我希望提高性能的最佳方法是添加索引,而不知道您的数据大小或分布我无法准确猜出您需要的确切索引,如果您自己运行查询显示实际执行计划SSMS会为你建议一个比我最好的猜测更好的指数。