我有以下查询,它执行得很好。 获得结果只需不到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 View中的列,则需要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))
答案 0 :(得分:1)
我在我的视图中交叉加入导致不必要的循环......