SQL查询需要更长的时间来执行

时间:2019-05-28 13:17:50

标签: sql sql-server-2008

我正在尝试执行查询。执行需要32分钟。我尝试过NOLOCK,但仅用3分钟对其进行了优化。 查询如下:-

SELECT PO.payday, PO.facility, PO.department, PO.workcenter, PO.shift, PO.team, PO.orderno, PO.fg_productno,               
       PO.sfg_productno, NoCarton = '1', NoPack = 5, NoCigarette = PO.nocigarette, VNC.sampleid, VNC.comment,               
       VNC.createdby, SampleDateTime = dbo.AF_GetUTCToLocal(VNC.UTCStartDate),               
       SampleTime = Cast(CONVERT(CHAR(10), VNC.utcstartdate, 101) AS DATETIME), VNC.ncgroup,  VNC.productcomponent,              
       VNC.nc, VNC.ncname, VNC.weightingfactor, VNC.ConsumerSensitive, VNC.[lowunits], VNC.[low %], VNC.[highunits],               
       VNC.[high %], VNC.vqi, VNC.deepcontrol,
       --Modification done for CHE10045964
       PO.ResourceClassName    
FROM   
(SELECT PayDay = WCS.payday, Facility = R.facility, Department = R.department, WorkCenter = R.workcenter,               
        Shift = WCS.workperiod, Team = SD.teamid, OrderNo = WO.orderno,               
        FG_ProductNo = P.productno + Isnull('.' + P.productrevision, ''),               
        SFG_ProductNo = CASE               
                        WHEN UCHSC.attribute_ != P.productno 
                        THEN UCHSC.attribute_               
                         ELSE NULL               
                         END,               
        WO.actualstartdate, WO.actualcompletiondate,
        NoCigarette = dbo.Af_getunitcharacteristicattribute(OD.unitid,'ZPPPI_ITEM_PACK') ,
        RC.Name as ResourceClassName              
        FROM   order_header OH               
               INNER JOIN dbo.wip_order WO ON OH.orderno = WO.orderno               
               INNER JOIN dbo.order_detail OD ON OD.orderno = WO.orderno               
               INNER JOIN dbo. product P ON WO.productid = P.id               
               INNER JOIN dbo.resource_ R ON R.resourcename = WO.resourcename AND R.resourcetype = WO.resourcetype 
               Inner Join dbo.Resource_Class RC ON R.ResourceClassID = RC.ID             
               INNER JOIN dbo.work_center_work_schedule WCS ON WO.oawipentityid = WCS.id             
               INNER JOIN at_work_center_work_schedule_detail SD ON WCS.id = SD.workcenterworkscheduleid               
               LEFT JOIN dbo.unit_characteristic UCHSC ON UCHSC.unitid = OD.unitid               
                                                       AND UCHSC.characteristic = 'ZPPPI_SPA_CIGARETTE'               
        WHERE  OH.ordertype = 23              
        GROUP  BY WCS.payday, R.facility, R.department, R.workcenter, WCS.workperiod, SD.teamid,               
                  WO.orderno, P.productno + Isnull('.' + P.productrevision, ''),               
                  CASE               
                    WHEN UCHSC.attribute_ != P.productno THEN UCHSC.attribute_               
                    ELSE NULL               
                  END,               
                  WO.actualstartdate, WO.actualcompletiondate,               
                  dbo.Af_getunitcharacteristicattribute(OD.unitid,'ZPPPI_ITEM_PACK'),
                  RC.Name
) PO INNER JOIN 
(
SELECT NC.orderno, NC.sampleid, NC.comment, NC.deepcontrol, NC.createdby, NC.utcstartdate, NC.ncgroup, NC.productcomponent,               
       NC.nc, NC.ncname, NC.weightingfactor, NC.ConsumerSensitive, NC.[lowunits], NC.[low %], NC.[highunits], NC.[high %],               
       VQI = Round(NC.weightingfactor * ( Log (1 + [low %] / 3.0 + [high %]) ), 0), 
       --Added By Sukanya Biswas(CHE10045964)
       NC.WorkCenter             
FROM  (SELECT orderno, b.sampleid, b.comment, b.deepcontrol, b.utcstartdate, b.ncgroup, b.productcomponent,               
              b.nc, b.ncname, b.weightingfactor, b.ConsumerSensitive, b.createdby,               
              [LowUnits] = Isnull(b.[1], 0),               
              [Low %] = CASE CONVERT(NVARCHAR(1),b.deepcontrol) + b.ncgroup               
                            WHEN '0Bundle'   THEN   CASE WHEN (Isnull(b.[1], 0) * 100) > 100 THEN 100 ELSE Isnull(b.[1], 0) * 100 END               
                            WHEN '1Bundle'   THEN   CASE WHEN (Isnull(b.[1], 0) * 100) > 100 THEN 100 ELSE Isnull(b.[1], 0) * 100 END                
                            WHEN '0Pack'      THEN   CASE WHEN (Isnull(b.[1], 0) * NoofCigarette ) > 100 THEN 100 ELSE Isnull(b.[1], 0) * NoofCigarette  END                
                            WHEN '1Pack'      THEN   CASE WHEN (Isnull(b.[1], 0) * NoofCigarette ) > 100 THEN 100 ELSE Isnull(b.[1], 0) * NoofCigarette  END               
                            WHEN '0Cigarette' THEN   CASE WHEN (Isnull(b.[1], 0) * 5  ) > 100 THEN 100 ELSE Isnull(b.[1], 0) * 5   END               
                            WHEN '1Cigarette' THEN   CASE WHEN (Isnull(b.[1], 0) * 1  ) > 100 THEN 100 ELSE Isnull(b.[1], 0) * 1   END               
                            WHEN '0Filter'   THEN   CASE WHEN (Isnull(b.[1], 0) * 10 ) > 100 THEN 100 ELSE Isnull(b.[1], 0) * 10  END               
                            WHEN '1Filter'   THEN   CASE WHEN (Isnull(b.[1], 0) * 10 ) > 100 THEN 100 ELSE Isnull(b.[1], 0) * 10  END               
                        ELSE Isnull(b.[1], 0)               
                        END, [HighUnits] = Isnull(b.[2], 0),        
             [High %] = CASE CONVERT(NVARCHAR(1),b.deepcontrol) + b.ncgroup               
                            WHEN '0Bundle'   THEN   CASE WHEN (Isnull(b.[2], 0) * 100) > 100 THEN 100 ELSE Isnull(b.[2], 0) * 100 END                
                            WHEN '1Bundle'   THEN   CASE WHEN (Isnull(b.[2], 0) * 100) > 100 THEN 100 ELSE Isnull(b.[2], 0) * 100 END                
                            WHEN '0Pack'      THEN   CASE WHEN (Isnull(b.[2], 0) * NoofCigarette ) > 100 THEN 100 ELSE Isnull(b.[2], 0) * NoofCigarette  END                
                            WHEN '1Pack'      THEN   CASE WHEN (Isnull(b.[2], 0) * NoofCigarette ) > 100 THEN 100 ELSE Isnull(b.[2], 0) * NoofCigarette  END                         
                            WHEN '0Cigarette' THEN   CASE WHEN (Isnull(b.[2], 0) * 5  ) > 100 THEN 100 ELSE Isnull(b.[2], 0) * 5   END               
                            WHEN '1Cigarette' THEN   CASE WHEN (Isnull(b.[2], 0) * 1  ) > 100 THEN 100 ELSE Isnull(b.[2], 0) * 1   END               
                            WHEN '0Filter'   THEN   CASE WHEN (Isnull(b.[2], 0) * 10 ) > 100 THEN 100 ELSE Isnull(b.[2], 0) * 10  END                
                            WHEN '1Filter'   THEN   CASE WHEN (Isnull(b.[2], 0) * 10 ) > 100 THEN 100 ELSE Isnull(b.[2], 0) * 10  END               
                            ELSE Isnull(b.[2], 0)               
                         END , 
                         --Added By Sukanya Biswas(CHE10045964)
                         B.WorkCenter             
            FROM (SELECT D .orderno, SampleID = DTS.id, 
            --Added By Sukanya Biswas(CHE10045964)
            DL.WorkCenter,             
                         Comment = CASE Isnull(DTS.comment_, '')               
                         WHEN '' THEN 'N/A'               
                         ELSE DTS.comment_               
                         END, UTCStartDate = DTS.actualstartdate, NC.ncgroup, NC.productcomponent, NC.nc, NC.ncname,               
                                          NC.weightingfactor, NC.ConsumerSensitive,              
                                          Severity = Isnull(QD.qualitydefectseverityid, 0),               
                                          QD.noofdefects, DTS.createdby, DeepControl = DTS.overridesumresults,                            
                                          NoofCigarette = dbo.Af_getunitcharacteristicattribute(OD.unitid,'ZPPPI_ITEM_PACK')                             
                  FROM   dbo.disposition D               
                  INNER JOIN dbo.disposition_line DL ON D .disposition = DL.disposition 
                  --Inner Join dbo.Work_Center WC ON DL.Workcenter = WC.Workcenter              
                  INNER JOIN dbo.disposition_test DT ON DT.disposition = D .disposition               
                                                     AND DT.linesequenceno =  DL.linesequenceno               
                  INNER JOIN dbo.disposition_test_sample DTS ON DTS.dispositiontestid = DT.id               
                  LEFT JOIN quality_defect QD ON QD.dispositiontestsampleid = DTS.id               
                  INNER JOIN dbo.AV_RPT_VSI_Nonconformity NC ON Isnull(QD.defectreasoncode, '00.00.00') = NC.nc              
                  LEFT JOIN ORDER_DETAIL OD ON OD.ORDERNO=D.ORDERNO            
                  WHERE  DL.linecode = 'VQ' 

                  ) A               
                  PIVOT (Sum(a.noofdefects) 
                  FOR severity IN ([1],[2])) 
          B) 
      NC

1 个答案:

答案 0 :(得分:0)

根据您的查询过滤器和所需的列,请在受尊重的列上建立索引。 默认情况下,SQL会在主键上创建聚簇索引,但是您可以在表上创建其他非聚簇索引,以加快执行速度。

您可能会找到此链接以供参考或使用Google,因为Internet上有数百篇相同的文章。 “ https://docs.microsoft.com/en-us/sql/t-sql/statements/create-index-transact-sql?view=sql-server-2017