2016-08-11 114 views
0

我們在舊版系統中發現了一個緩慢的查詢。我在查詢中看到的是重複的片段。下面是完整的查詢:查詢優化。重複的子查詢

DECLARE @SellerId INT; 
DECLARE @DateFrom DATETIME; 
DECLARE @DateTo DATETIME; 

SET @SellerId = 5396884; 
SET @DateFrom = '2016-01-05'; 
SET @DateTo = '2016-10-08'; 

DECLARE @CurrentDate DATETIME; 
SET @CurrentDate = GETDATE(); 



CREATE TABLE #ReportDate (codes INT, dates DATETIME); 
DECLARE @dif as INT; 
DECLARE @cont as INT; 
DECLARE @currdate as DATETIME; 
SET @dif = DATEDIFF(day, @DateFrom, @DateTo); 
SET @cont = 1; 
SET @currdate = @DateFrom - 1; 
WHILE (@cont <= @dif + 1) 
BEGIN 
    SET @currdate = DATEADD(DAY, 1, @currdate); 
    INSERT INTO #ReportDate VALUES (@cont, @currdate); 
    SET @cont = @cont + 1; 
END 


/* HOW TO OPTIMIZE THIS ONE? */ 
SELECT 
     #ReportDate.dates as valid_date, 
     (
      SELECT 

      COUNT(DISTINCT(nonCancelledSales.num_remito)) as actives 

      FROM  
       (

        SELECT * 

        FROM salesView 

        WHERE 

         salesView.sell_id NOT IN 
          (
           SELECT sell_id 

           FROM salesStates 

           WHERE 
            salesStates.aborted = 1 
          ) 

       ) nonCancelledSales 

      WHERE 
       nonCancelledSales.seller_id = @SellerId AND 
       nonCancelledSales.cancelled = 0 AND 
       nonCancelledSales.void = 0 AND 
       nonCancelledSales.hasDiscount = 0 AND 
       nonCancelledSales.dateOfSale <= #ReportDate.dates AND 
       nonCancelledSales.currentState = (SELECT MAX(hveest.date) 

               FROM salesStates hveest 

               WHERE 
                hveest.sell_id = nonCancelledSales.sell_id AND 
                hveest.date <= #ReportDate.dates) AND 
       nonCancelledSales.lastProductDate = (SELECT  MAX(hvepro.date) 

               FROM productHistory hvepro 

               WHERE 
                hvepro.sell_id = nonCancelledSales.sell_id AND 
                hvepro.date <= #ReportDate.dates) 

     ) total_actives, 

     (
      SELECT 

      ISNULL(SUM(nonCancelledSales.paymentValue),0) as active 

      FROM  
       (

        SELECT * 

        FROM salesView 

        WHERE 

         salesView.sell_id NOT IN 
          (
           SELECT sell_id 

           FROM salesStates 

           WHERE 
            salesStates.aborted = 1 
          ) 

       ) nonCancelledSales 

      WHERE 
       nonCancelledSales.seller_id = @SellerId AND 
       nonCancelledSales.cancelled = 0 AND 
       nonCancelledSales.void = 0 AND 
       nonCancelledSales.hasDiscount = 0 AND 
       nonCancelledSales.dateOfSale <= #ReportDate.dates AND 
       nonCancelledSales.currentState = (SELECT MAX(hveest.date) 

               FROM salesStates hveest 

               WHERE 
                hveest.sell_id = nonCancelledSales.sell_id AND 
                hveest.date <= #ReportDate.dates) AND 
       nonCancelledSales.lastProductDate = (SELECT  MAX(hvepro.date) 

               FROM productHistory hvepro 

               WHERE 
                hvepro.sell_id = nonCancelledSales.sell_id AND 
                hvepro.date <= #ReportDate.dates)    
     ) active 
FROM 
     #ReportDate 
GROUP BY 
     #ReportDate.dates 



DROP TABLE #ReportDate 

這裏有兩個複製片段我看到:

(
      SELECT 

      COUNT(DISTINCT(nonCancelledSales.num_remito)) as actives 

      FROM  
       (

        SELECT * 

        FROM salesView 

        WHERE 

         salesView.sell_id NOT IN 
          (
           SELECT sell_id 

           FROM salesStates 

           WHERE 
            salesStates.aborted = 1 
          ) 

       ) nonCancelledSales 

      WHERE 
       nonCancelledSales.seller_id = @SellerId AND 
       nonCancelledSales.cancelled = 0 AND 
       nonCancelledSales.void = 0 AND 
       nonCancelledSales.hasDiscount = 0 AND 
       nonCancelledSales.dateOfSale <= #ReportDate.dates AND 
       nonCancelledSales.currentState = (SELECT MAX(hveest.date) 

               FROM salesStates hveest 

               WHERE 
                hveest.sell_id = nonCancelledSales.sell_id AND 
                hveest.date <= #ReportDate.dates) AND 
       nonCancelledSales.lastProductDate = (SELECT  MAX(hvepro.date) 

               FROM productHistory hvepro 

               WHERE 
                hvepro.sell_id = nonCancelledSales.sell_id AND 
                hvepro.date <= #ReportDate.dates) 

     ) total_actives, 

     (
      SELECT 

      ISNULL(SUM(nonCancelledSales.paymentValue),0) as active 

      FROM  
       (

        SELECT * 

        FROM salesView 

        WHERE 

         salesView.sell_id NOT IN 
          (
           SELECT sell_id 

           FROM salesStates 

           WHERE 
            salesStates.aborted = 1 
          ) 

       ) nonCancelledSales 

      WHERE 
       nonCancelledSales.seller_id = @SellerId AND 
       nonCancelledSales.cancelled = 0 AND 
       nonCancelledSales.void = 0 AND 
       nonCancelledSales.hasDiscount = 0 AND 
       nonCancelledSales.dateOfSale <= #ReportDate.dates AND 
       nonCancelledSales.currentState = (SELECT MAX(hveest.date) 

               FROM salesStates hveest 

               WHERE 
                hveest.sell_id = nonCancelledSales.sell_id AND 
                hveest.date <= #ReportDate.dates) AND 
       nonCancelledSales.lastProductDate = (SELECT  MAX(hvepro.date) 

               FROM productHistory hvepro 

               WHERE 
                hvepro.sell_id = nonCancelledSales.sell_id AND 
                hvepro.date <= #ReportDate.dates)    
     ) active 

是否有必要完全複製的查詢?在第一個他得到:

COUNT(DISTINCT(nonCancelledSales.num_remito)) as actives 

上第二個:

ISNULL(SUM(nonCancelledSales.paymentValue),0) as active 

我想必須有某種方式來重寫查詢,但我不知道怎麼樣。

+0

看起來像一次只有一個查詢,這可以解釋'GROUP BY#ReportDate.dates' – JamieD77

+0

您也可以通過刪除該循環並使用計數表來填充日期列表來加快速度。這不太可能是最差的部分性能明智的,但它是基於而不是一個循環,這是非常容易的。這是一篇很好的文章,解釋統計表以及他們如何取代循環。 http://www.sqlservercentral.com/articles/T-SQL/62867/ –

+0

@ JamieD77你說這個組是多餘的? –

回答

0

如果您使用OUTER APPLY,您可以合併這些。

的理念是:

SELECT . . ., x.actives, x.active 
FROM #ReportDate OUTER APPLY 
    (SELECT COUNT(DISTINCT(nonCancelledSales.num_remito)) as actives, 
      COALESCE(SUM(nonCancelledSales.paymentValue), 0) as active 
     . . . -- rest of query here 
    ) x; 

在這種情況下,OUTER APPLY是很像FROM子句可以返回多行相關子查詢。

+0

因此,使用OUTER APPLY是重寫此查詢的唯一且最好的方法?我認爲外部應用是用於表值函數 –

+0

@ StephenH.Anderson。 。 。這是編寫邏輯的最明顯的方法。子查詢只能返回一個值,所以它會解決這個問題。我不明白這個邏輯,所以可能有一些方法可以更簡單地寫出來。但'APPLY'實現了所謂的「橫向連接」。表值函數只是一個應用程序。 –