2017-06-07 65 views
1

我寫過這個查詢,並且因爲我使用了臨時表(第一次),所以它大量地殺死了查詢的性能。臨時表查殺性能

這是查詢。你能發現我正在犯的一個巨大的錯誤嗎?

SET NOCOUNT ON; 
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; 

DECLARE @StartDate DATETIME 
SET @StartDate = '20160720'; 
DECLARE @EndDate DATETIME 
SET @EndDate = '20160729 23:59:59'; 

Select 
SUM(c.[Chargeable Weight]) AS [1stchargeableweights], 
mm.MovementRef [movementref], 
MAX (ts.systemstamp) [lastdate] 
Into #1schargslack 

            FROM  dbo.whsConsignment wc 
                 INNER JOIN dbo.cgtConsignment c ON c.[Consignment Reference] = wc.ConsignmentReference 
                 INNER JOIN dbo.tsAdditionalColInfo ts on ts.[ConsignmentReference]= wc.ConsignmentReference 
                 inner join dbo.movConLink m on m.ConsignmentReference = c.[Consignment Reference] 
                 inner join dbo.movMovement mm on mm.MovementID = m.MovementID 
                 --INNER JOIN dbo.whsHeader wh ON wh.WhsHeaderID = wc.whsHeaderID 
                            --AND wc.whsHeaderID = wh.WhsHeaderID 
                            AND wc.StatusCode = 'NL' 
                            AND c.[Service Type] = 'F' 
                            AND ts.SlackNoSlack = 'slack' 
                            or ts.slacknoslack like 'slack - tripped%' 
                            GROUP by mm.MovementRef 
Select 
SUM(c.[Chargeable Weight]) AS [1stchargeableweightns], 
mm.MovementRef [movementref], 
MAX (ts.systemstamp) [lastdate] 
Into #1chargns 


            FROM  dbo.whsConsignment wc              INNER JOIN dbo.cgtConsignment c ON c.[Consignment Reference] = wc.ConsignmentReference 
                 INNER JOIN dbo.tsAdditionalColInfo ts on ts.[ConsignmentReference]= wc.ConsignmentReference 
                 inner join dbo.movConLink m on m.ConsignmentReference = c.[Consignment Reference] 
                 inner join dbo.movMovement mm on mm.MovementID = m.MovementID 
                 --INNER JOIN dbo.whsHeader wh ON wh.WhsHeaderID = wc.whsHeaderID 
                            --AND wc.whsHeaderID = wh.WhsHeaderID 
                            AND wc.StatusCode = 'NL' 
                            AND c.[Service Type] = 'F' 
                            and ts.SlackNoSlack like 'no slack%' 
                            GROUP BY mm.MovementRef 
Select 
SUM(c.[Chargeable Weight]) AS [2ndchargeableweights], 
mm.MovementRef [movementref], 
MAX (ts.systemstamp) [lastdate] 
Into #2chargs 

            FROM  dbo.whsConsignment wc 
                 INNER JOIN dbo.cgtConsignment c ON c.[Consignment Reference] = wc.ConsignmentReference 
                 INNER JOIN dbo.tsAdditionalColInfo ts on ts.[ConsignmentReference]= wc.ConsignmentReference 
                 inner join dbo.movConLink m on m.ConsignmentReference = c.[Consignment Reference] 
                 inner join dbo.movMovement mm on mm.MovementID = m.MovementID 
                 -- INNER JOIN dbo.whsHeader wh ON wh.WhsHeaderID = wc.whsHeaderID 
                               -- AND wc.whsHeaderID = wh.WhsHeaderID 
                               AND wc.StatusCode = 'NL' 
                               AND c.[Service Type] = 'C' 
                               AND ts.SlackNoSlack = 'slack' 
                               or ts.slacknoslack like 'slack - tripped%' 
                               GROUP BY mm.MovementRef                                                       
Select 
SUM(c.[Chargeable Weight]) AS [2ndchargeableweightns], 
mm.MovementRef [movementref], 
MAX (ts.systemstamp) [lastdate] 
Into #2chargns 

            FROM  dbo.whsConsignment wc 
                 INNER JOIN dbo.cgtConsignment c ON c.[Consignment Reference] = wc.ConsignmentReference 
                 INNER JOIN dbo.tsAdditionalColInfo ts on ts.[ConsignmentReference]= wc.ConsignmentReference 
                 inner join dbo.movConLink m on m.ConsignmentReference = c.[Consignment Reference] 
                 inner join dbo.movMovement mm on mm.MovementID = m.MovementID 
                 -- INNER JOIN dbo.whsHeader wh ON wh.WhsHeaderID = wc.whsHeaderID 
                               -- AND wc.whsHeaderID = wh.WhsHeaderID 
                               AND wc.StatusCode = 'NL' 
                               AND c.[Service Type] = 'C' 
                               and ts.SlackNoSlack like 'no slack%' 
                               group by mm.MovementRef 
Select 
SUM(c.[Chargeable Weight]) AS [2ndchargeableweightlate], 
mm.MovementRef [movementref], 
MAX (ts.systemstamp) [lastdate] 
Into #2charglate 

            FROM  dbo.whsConsignment wc 
                 INNER JOIN dbo.cgtConsignment c ON c.[Consignment Reference] = wc.ConsignmentReference 
                 INNER JOIN dbo.tsAdditionalColInfo ts on ts.[ConsignmentReference]= wc.ConsignmentReference 
                 inner join dbo.movConLink m on m.ConsignmentReference = c.[Consignment Reference] 
                 inner join dbo.movMovement mm on mm.MovementID = m.MovementID 
                 -- INNER JOIN dbo.whsHeader wh ON wh.WhsHeaderID = wc.whsHeaderID 
                               -- AND wc.whsHeaderID = wh.WhsHeaderID 
                               AND wc.StatusCode = 'NL' 
                               AND c.[Service Type] = 'C'                     
                               and ts.SlackNoSlack like '%late%' 
                               group by mm.movementref 
    SELECT --wh.WhsHeaderID, 
       mu.Number AS Unit, 
     m.MovementRef AS Reference, 
     wh.ArrDepDate AS [Departure Date], 
     wh.LastScannedTime AS [Last Scan], 
     CONVERT(VARCHAR(5), wh.ArrDepDate, 108) AS [Scheduled Departure (h)], 
     CONVERT(VARCHAR(5), wh.LastScannedTime, 108) AS [Last Scan (h)],     
     CASE 
      WHEN 
      (
       m.DestinationPoint = 'SF' 
       AND DATEDIFF(MINUTE, wh.ArrDepDate, wh.LastScannedTime) > 180 
      ) 
      OR (m.DestinationPoint <> 'SF' AND DATEDIFF(MINUTE, wh.ArrDepDate, wh.LastScannedTime) > 20) THEN 
       'Late' 
      ELSE 
       'On Time' 
     END AS Status, 
     CAST(ch.ChargeableWt + ISNULL(LO.LeftOffWt, 0) AS DECIMAL(18, 2)) AS [Planned Chargeable Wt], 
     Cast (isnull ([1stchargeableweights], 0) as decimal (18, 2)) as [1st Dep Left off Chargeable Wt With Slack], 
     Cast (isnull ([1stchargeableweightns], 0) as decimal (18, 2)) as [1st Dep Left off Chargeable Wt No Slack], 
     Cast (isnull ([2ndchargeableweights], 0) as decimal (18, 2)) as [2nd Dep Left off Chargeable Wt With Slack], 
     Cast (isnull ([2ndchargeableweightns], 0) as decimal (18, 2)) as [2nd Dep Left off Chargeable Wt No Slack], 
     Cast (isnull ([2ndchargeableweightlate], 0) as decimal (18, 2)) as [2nd Dep Left off Chargeable Wt Late], 
     CAST(ch.ChargeableWt AS DECIMAL(18, 2)) AS [Actual Chargeable Wt], 

     CASE 
      WHEN m.DestinationPoint IN ('AT', 'FR', 'PL', 'NL', 'BE', 'ES', 'IT', 'CH', 'CZ', 'TU', 'MT', 'DE') THEN 
       22000 
      ELSE 
       20000 
     END AS Target, 
     CASE 
      WHEN m.DestinationPoint IN ('AT', 'FR', 'PL', 'NL', 'BE', 'ES', 'IT', 'CH', 'CZ', 'TU', 'MT', 'DE') THEN 
       CAST(ch.ChargeableWt - 22000 AS DECIMAL(18, 2)) 
      ELSE 
       CAST(ch.ChargeableWt - 20000 AS DECIMAL(18, 2)) 
     END AS Variance 
     --ISNULL(
     --   STUFF(
     --         (
     --           SELECT ', ' + COALESCE(R.Region, 'Region not found') + ': ' + SUBSTRING(cnor.PostCode,1,CHARINDEX( ' ',cnor.PostCode + ' ') - 1) + 
     --                 ' (' + COALESCE(DC.Description, 'NA') + ')' 
     --           FROM dbo.whsConsignment wc 
     --             INNER JOIN dbo.cgtConsignment c ON c.[Consignment Reference] = wc.ConsignmentReference AND c.Cancelled = 0 
     --             INNER JOIN dbo.genAddress cnor ON cnor.AddressID = c.Consignor 
     --             LEFT OUTER JOIN dbo.disConsignment AS DC ON DC.ConsignmentRef = wc.ConsignmentReference AND DC.WhsHeaderID = wc.whsHeaderID 
     --             LEFT JOIN dbo.lstPostcode P ON P.Postcode = cnor.PostcodePrefix 
     --             LEFT JOIN dbo.lstPostcodeRegion R ON R.RegionID = P.RegionID 
     --           WHERE wc.whsHeaderID = wh.WhsHeaderID AND wc.StatusCode = 'NL' 
     --           FOR XML PATH('') 
     --          ), 
     --     1,1,'' 
     --    ) 
     --  ,'') AS [Postcodes & Discrepancies]  
    FROM dbo.movMovement m 
    inner join #1schargslack on #1schargslack.movementref = m.MovementRef 
    inner join #1chargns on #1chargns.movementref = m.MovementRef 
    inner join #2chargs on #2chargs.movementref = m.MovementRef 
    inner join #2chargns on #2chargns.MovementRef = m.MovementRef 
    inner join #2charglate on #2charglate.movementref = m.MovementRef 
     INNER JOIN dbo.movUnit mu ON mu.UnitID = m.TrailerID 
     INNER JOIN dbo.whsHeader wh ON wh.RunMovID = m.MovementID AND wh.LocalDepotCode = 'NOT' AND wh.WhsJobType = 'CL' AND wh.WhsJobStatus = 'COMP' 
     LEFT JOIN dbo.whsHeaderComments whc ON whc.ID = wh.WhsHeaderID 
     INNER JOIN (
             SELECT MovementID, 
               SUM(c.[Chargeable Weight]) AS ChargeableWt 
             FROM dbo.movConLink mc 
               INNER JOIN dbo.cgtConsignment c ON c.[Consignment Reference] = mc.ConsignmentReference AND c.Cancelled = 0 
             GROUP BY mc.MovementID 
           ) ch ON ch.MovementID = m.MovementID 
     LEFT JOIN (
            SELECT wc.whsHeaderID, 
             SUM(c.[Chargeable Weight]) AS LeftOffWt 
            FROM dbo.whsConsignment wc 
             INNER JOIN dbo.cgtConsignment c ON c.[Consignment Reference] = wc.ConsignmentReference AND c.Cancelled = 0 
             INNER JOIN dbo.genAddress cnor ON cnor.AddressID = c.Consignor 
            WHERE StatusCode IN ('NL') 
            GROUP BY wc.whsHeaderID 
          ) LO ON LO.whsHeaderID = wh.WhsHeaderID 
    WHERE wh.ArrDepDate 
    BETWEEN @StartDate AND @EndDate 
    ORDER BY m.MovementRef; 
+2

堆棧溢出不能這樣工作。您無法轉儲數百行SQL代碼,並希望任何人都能對其進行篩選。 _編輯你的問題,也許讓我們看看哪一個查詢有臨時表的問題。 –

+0

與你的問題沒有關係,但是我不認爲這是一個好的設置,如果你是新的SQL Server(而不是)'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' – Horia

+0

嗨Horia,你可以請擴展一下嗎?爲什麼SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;不合適?謝謝! –

回答

0

如果你正在填充一個臨時表,然後只從它調用一次,你可以看看使用CTE的。它將爲SQL Server填充表格節省很多時間。

+1

輝煌,謝謝dbajtr!我會考慮使用CTE的:) –

+0

不用擔心 - 如果你使用臨時表記得在查詢結束時刪除它們 – dbajtr