1

我有一个 SP,它从包含 UNION 的查询中返回分页数据。这正在杀死我的数据库并且有时需要 30 秒才能运行,我在这里遗漏了一些明显的东西吗?我能做些什么来提高它的性能?

涉及的表:产品、类别、类别产品

目标:任何不在类别中或已从类别中删除的产品联合当前在一个类别中的所有产品,并为 Web 服务在这些产品上翻页。

我加入的所有列都有索引,数据库中有 427,996 个产品、6148 个类别和 409,691 个类别产品。

这是我的查询,运行时间在 6 到 30 秒之间:

    SELECT * FROM (
    SELECT ROW_NUMBER() OVER(ORDER BY Products.ItemID, Products.ManufacturerID) AS RowNum, *
        FROM
        (
        SELECT  Products.*,
              CategoryID = NULL, CategoryName = NULL, 
              CategoryProductID = NULL, 
                          ContainerMinimumQuantity = 
                            CASE  COALESCE(Products.ContainerMinQty, 0)
                                WHEN 0 THEN Products.OrderMinimumQuantity
                                ELSE Products.ContainerMinQty
                            END 
                            Products.IsDeleted, 
                           SortOrder = NULL
    FROM    CategoryProducts RIGHT OUTER JOIN   Products 
        ON CategoryProducts.ManufacturerID = Products.ManufacturerID 
            AND CategoryProducts.ItemID = Products.ItemID
            WHERE     (Products.ManufacturerID = @ManufacturerID)
                AND (Products.ModifiedOn > @tStamp )
                 AND  ((CategoryProducts.IsDeleted = 1) OR (CategoryProducts.IsDeleted IS NULL))

                UNION

                SELECT Products.*, 
 CategoryProducts.CategoryID , CategoryProducts.CategoryName, 
          CategoryProducts.CategoryProductID , 
                          ContainerMinimumQuantity = 
                            CASE  COALESCE(Products.ContainerMinQty, 0)
                                WHEN 0 THEN Products.OrderMinimumQuantity
                                ELSE Products.ContainerMinQty
                            END 
                            CategoryProducts.IsDeleted,
                           CategoryProducts.SortOrder
    FROM      Categories INNER JOIN
                          CategoryProducts ON Categories.CategoryID = CategoryProducts.CategoryID INNER JOIN
                          Products ON CategoryProducts.ManufacturerID = Products.ManufacturerID
             AND CategoryProducts.ItemID = Products.ItemID
    WHERE     (Products.ManufacturerID = @ManufacturerID) 
            AND  (Products.ModifiedOn > @tStamp OR  CategoryProducts.ModifiedOn > @tStamp))  
            AS Products) AS C 
        WHERE RowNum >= @StartRow AND RowNum <= @EndRow

任何见解将不胜感激。

4

1 回答 1

0

如果我正确阅读了您的情况,那么有两个不同查询的唯一原因是处理丢失/删除的 CategoryProducts。我试图通过左连接来解决这个问题,IsDeleted = 0以将所有已删除的 CategoryProducts 变为空值,因此我不必再次测试它们。对于您希望检索的丢失/删除的 Categoryproducts,ModifiedOn 部分对 null 进行了另一次测试。

select *
from (
    SELECT 
      Products.*, 
      -- Following three columns will be null for deleted/missing categories
      CategoryProducts.CategoryID, 
      CategoryProducts.CategoryName, 
      CategoryProducts.CategoryProductID , 
      ContainerMinimumQuantity = COALESCE(nullif(Products.ContainerMinQty, 0),
                                          Products.OrderMinimumQuantity),
      CategoryProducts.IsDeleted,
      CategoryProducts.SortOrder,
      ROW_NUMBER() OVER(ORDER BY Products.ItemID, 
                                 Products.ManufacturerID) AS RowNum
    FROM Products 
      LEFT JOIN CategoryProducts 
         ON CategoryProducts.ManufacturerID = Products.ManufacturerID
        AND CategoryProducts.ItemID = Products.ItemID
    -- Filter IsDeleted in join so we get nulls for deleted categories
    -- And treat them the same as missing ones
        AND CategoryProducts.IsDeleted = 0
      LEFT JOIN Categories
         ON Categories.CategoryID = CategoryProducts.CategoryID 
    WHERE Products.ManufacturerID = @ManufacturerID
      AND (Products.ModifiedOn > @tStamp 
        -- Deleted/missing categories
           OR CategoryProducts.ModifiedOn is null
           OR CategoryProducts.ModifiedOn > @tStamp)
    ) C
WHERE RowNum >= @StartRow AND RowNum <= @EndRow

第三次看,除了作为 CategoryProducts 的过滤器之外,我根本没有使用 Category。如果是这种情况,则应将第二个 LEFT JOIN 更改为 INNER JOIN,并且此部分应括在括号中。

于 2012-04-18T19:10:34.067 回答