减少查询执行时间

时间:2019-06-21 07:10:40

标签: sql sql-server

我已经编写了查询,但是当我执行查询时,它花费了将近5分钟的执行时间。我必须减少这种情况,因此请指导如何实现。

DECLARE @start AS DATETIME,
        @end   AS DATETIME

SET @start = '06/20/2019 06:00:00'
SET @end = '06/21/2019 06:00:00'

SELECT Maker,Brand,[Opn No],Shift,[300 Cig Weight],Circumference,[SD(Cir.)],IWV,
       PD,NTM,
       Mass,Volume,[Actual Density],( [Actual Density] * ( CD ) ) AS
                                    [Corrected Density],CD,Height,Round(
       ActualRadius, 4) AS [Actual Radius],Round(NetRadius, 4) AS [Net Radius]
FROM   (SELECT A.Maker,A.Brand,A.[Opn No],A.Shift,
               Round(Avg(A.[300 Cig Weight]), 4) AS
                      [300 Cig Weight],
                      Round(Avg(A.Circumference), 4) AS Circumference,
                      Round(Avg(A.[SD(Cir.)]), 4) AS [SD(Cir.)],
                      Round(Avg(A.IWV), 4) AS IWV,Round(Avg(A.PD), 4) AS PD,
                      Round(Avg(A.NTM), 4) AS NTM,Avg(A.IWV - A.NTM) AS Mass,
                      Avg(
                      ( 3.14 * ( Square(( A.ActualRadius - 0.042 ) / 10) ) ) *
                      A.Height) AS
                      Volume,Avg(A.ActualRadius) AS ActualRadius,Avg(
                      Square(( A.ActualRadius - 0.042 ) / 10)) AS
                      NetRadius,Avg(( ( A.IWV - A.NTM ) / (
                              ( 3.14 *
               ( Square(( A.ActualRadius - 0.042 ) / 10) ) ) *
                              A.Height ) ) * 1000) AS [Actual Density],Avg(
                      ( 100 - B.Final_Weight ) / 86.5) AS
                      [CD],Avg(A.Height) AS Height
        FROM   (SELECT CASE
                         WHEN Ci.Machine = 'LOGA3D85' THEN 'LOGA 3D85'
                         WHEN Ci.Machine = 'LOGA2_RSFT' THEN 'LOGA 2 MAX'
                         WHEN Ci.Machine = 'PROTOS_2C' THEN 'P2C'
                       END AS Maker,Ci.BrandName AS Brand,Ci.OpnNo AS [Opn No],
                       Ci.Shift,
                               GW.Weight AS [300 Cig Weight],Ci.Mean_SizeL AS
                               Circumference,( Ci.Mean_SizeL / 6.28 ) AS
                                             ActualRadius,
                               Ci.StDev_SizeL AS [SD(Cir.)],
                       QTM.[Mean_Weight ] AS IWV,
                               PDO.Mean_PDOpen AS PD,BBM.NTM_Weight AS NTM,
                       BBM.Height
                FROM   Circum AS Ci
                       FULL OUTER JOIN GroupWeight AS GW
                                    ON GW.OpnNo = Ci.OpnNo
                       FULL OUTER JOIN PDOpen AS PDO
                                    ON PDO.OpnNo = GW.OpnNo
                       FULL OUTER JOIN QTMWeight AS QTM
                                    ON QTM.OpnNo = GW.OpnNo
                       FULL OUTER JOIN BlendBrandMaster AS BBM
                                    ON BBM.BrandName COLLATE
                                       sql_latin1_general_cp1_ci_as =
                                       Ci.BrandName COLLATE
                                       sql_latin1_general_cp1_ci_as
                WHERE  ( Ci.StartDateTime >= CONVERT(DATETIME, @start, 105) )
                       AND ( Ci.StartDateTime < CONVERT(DATETIME, @end, 105) )
                       AND ( Ci.Shift IN ( 'Early' ) )) AS A
               LEFT OUTER JOIN (SELECT Avg(Final_Weight) AS Final_Weight,Shift
                                FROM   MoistureData
                                WHERE  ( Location LIKE '%Making%' )
                                       AND BeforeOvenDateTime >=
                                           CONVERT(DATETIME, @start, 105)
                                       AND BeforeOvenDateTime <
                                           CONVERT(DATETIME, @end,
                                           105)
                                       AND Shift IN ( 'Early' )
                                GROUP  BY Shift) AS B
                            ON A.Shift COLLATE sql_latin1_general_cp1_ci_as =
                               B.Shift COLLATE sql_latin1_general_cp1_ci_as
        GROUP  BY A.Maker,A.Brand,A.[Opn No],A.Shift) AS C  

0 个答案:

没有答案