0

我有以下查询,它执行得很好。不到 1 秒即可获得结果。

SELECT DISTINCT Items.ImageID AS ImgID, Items.AddDate, MIN(ra1.DescPriority) AS Pri
FROM            Items 
INNER JOIN      Attribs AS ra0 ON Items.ImageID = ra0.ImageID 
INNER JOIN      Attribs AS ra1 ON ra0.ImageID = ra1.ImageID 
LEFT OUTER JOIN v_priceOrder ON Items.ImageID = v_priceOrder.ImageID
WHERE        (Items.deleted NOT IN (1, 2)) 
GROUP BY Items.ImageID, Items.AddDate
ORDER BY pri, Items.AddDate DESC

但是,如果我尝试在选择列表中添加 PriceOrder 视图中的列,则需要 10 秒才能获得结果。

SELECT DISTINCT Items.ImageID AS ImgID, Items.AddDate, MIN(ra1.DescPriority) AS Pri,
    v_priceOrder.PriceOrder
FROM            Items 
INNER JOIN      Attribs AS ra0 ON Items.ImageID = ra0.ImageID 
INNER JOIN      Attribs AS ra1 ON ra0.ImageID = ra1.ImageID 
LEFT OUTER JOIN v_priceOrder ON Items.ImageID = v_priceOrder.ImageID
WHERE        (Items.deleted NOT IN (1, 2)) 
GROUP BY Items.ImageID, Items.AddDate, v_priceOrder.PriceOrder
ORDER BY v_priceOrder.PriceOrder, pri, Items.AddDate DESC

有没有办法改进这个查询?在执行计划中,我只看到 0 或 1% 的成本。我正在使用 MS SQL Server 2008 R2。

更新: 我的观点有问题。运行需要 8 秒...这是查询。

SELECT     dbo.Items.ImageID, 
                          CASE WHEN dbo.Items.SubcategoryID2 = 'STO' THEN dbo.v_DisplayStockPrice.DisplayPrice WHEN dbo.Items.SubcategoryID2 = 'ORD' THEN dbo.v_FinalPriceFactor.[14kyFactorOrder]
                          WHEN dbo.Items.SubcategoryID2 IS NULL THEN dbo.v_FinalPriceFactor.[14kyCustomLow] END AS PriceOrder
FROM       dbo.v_FinalPriceFactor RIGHT OUTER JOIN
                          dbo.Items ON dbo.v_FinalPriceFactor.ImageID = dbo.Items.ImageID LEFT OUTER JOIN
                          dbo.v_DisplayStockPrice ON dbo.Items.ImageID = dbo.v_DisplayStockPrice.ImageID
WHERE     (dbo.Items.deleted NOT IN (1, 2))
4

1 回答 1

1

我在我的视图中交叉加入导致不必要的循环......

于 2012-07-01T20:56:35.267 回答