简体   繁体   English

SQL Server:提高内部查询的性能

[英]SQL Server : improve performance of inner query

I am trying to improve the performance of my query by reducing the inner queries. 我试图通过减少内部查询来提高查询的性能。

I will need the first one as it returns the ReasonTUM. 我将需要第一个,因为它返回ReasonTUM。

However, I would like to use a case with the ReasonTUM so that it returns a number as well. 但是,我想使用ReasonTUM的一个案例,以便它也返回一个数字。

How can I achieve this? 我怎样才能做到这一点? I've tried referencing mms.sMachineStateName in the original query but it doesn't detect the column. 我尝试在原始查询中引用mms.sMachineStateName但它没有检测到列。

The only other way I can think of is to use another sub query but this is taking about 1min to return 300 rows due to the joins. 我能想到的另一种方法是使用另一个子查询,但由于连接,这需要大约1分钟来返回300行。

declare @ReportingStart datetime = '20160917 07:00'
declare @ReportingEnd datetime = '20160918 07:00'

SELECT 
      [sWorkcellDescription]
      ,[tStart]
      ,[dDurationSeconds]/60 as Duration_m
      ,[sStateDescription]
      ,datepart(hh,tstart) as myHr
      ,case when convert(time,tstart)< '07:00' then dateadd(dd,-1,convert(date,tstart)) else convert(date,tstart) end as myDate,
        cast(dateadd(hour,datepart(hh,tstart),0) as datetime) as dispTime,
        (
            select top 1 mms.sMachineStateName
            from OEEEvent oe
            inner join RSBizWare.dbo.OEEConfigEvent ce on oe.lOEEConfigEventId = ce.lOEEConfigEventId
            inner join RSBizWare.dbo.OEELOVCodeVal rs on oe.sStartVal = rs.sDescription and ce.lOEEIntRSSqlId=rs.lOEELOVCodeId
            inner join RSBizWare.dbo.OEEStateConfig mms on rs.lMachineState = mms.lOEEStateConfigId
            where qq.tStart between oe.tStart and oe.tEnd and oe.sPartId='Ore-Hoist'
            order by qq.tStart asc
        ) as ReasonTUM,
                (
            select top 1 case
        when mms.sMachineStateName = 'Production' then '1' 
        when mms.sMachineStateName = 'Unscheduled Production' then '2'
        when mms.sMachineStateName = 'Idle time' then '3'
        when mms.sMachineStateName = 'Opportune Maintenance' then '4'
        when mms.sMachineStateName = 'Planned External Downtime' then '5'
        when mms.sMachineStateName = 'Planned External Downtime' then '5'
        when mms.sMachineStateName = 'Planned Maintenance Mechanical' then '5'
        when mms.sMachineStateName = 'Planned Maintenance Electrical' then '6'
        when mms.sMachineStateName = 'Unplanned Downtime Operational' then '7'
        when mms.sMachineStateName = 'Unplanned Downtime Mechanical' then '8'
        when mms.sMachineStateName = 'Unplanned Downtime Electrical' then '9'
        else '99' end
            from OEEEvent oe
            inner join RSBizWare.dbo.OEEConfigEvent ce on oe.lOEEConfigEventId = ce.lOEEConfigEventId
            inner join RSBizWare.dbo.OEELOVCodeVal rs on oe.sStartVal = rs.sDescription and ce.lOEEIntRSSqlId=rs.lOEELOVCodeId
            inner join RSBizWare.dbo.OEEStateConfig mms on rs.lMachineState = mms.lOEEStateConfigId
            where qq.tStart between oe.tStart and oe.tEnd and oe.sPartId='Ore-Hoist'
        ) as rank
FROM 
    [RSBizWare].[dbo].[OEEQStateData] qq
WHERE 
    (tstart >= @ReportingStart AND tStart < @ReportingEnd) 
    AND sWorkcellDescription = 'Hoisting' 
    AND dDurationSeconds > 5
ORDER BY
    tStart ASC

Use OUTER APPLY to avoid calling the sub-query twice 使用OUTER APPLY避免两次调用sub-query

SELECT [sworkcelldescription], 
       [tstart], 
       [ddurationseconds] / 60                                  AS Duration_m, 
       [sstatedescription], 
       Datepart(hh, tstart)                                     AS myHr, 
       CASE 
         WHEN CONVERT(TIME, tstart) < '07:00' THEN Dateadd(dd, -1, 
                                                   CONVERT(DATE, tstart 
                                                   )) 
         ELSE CONVERT(DATE, tstart) 
       END                                                      AS myDate, 
       Cast(Dateadd(hour, Datepart(hh, tstart), 0) AS DATETIME) AS dispTime, 
       OA.reasontum, 
       OA.[rank] 
FROM   [RSBizWare].[dbo].[oeeqstatedata] qq 
       OUTER apply (SELECT TOP 1 mms.smachinestatename, 
                                 CASE mms.smachinestatename 
                                   WHEN 'Production' THEN '1' 
                                   WHEN 'Unscheduled Production' THEN '2' 
                                   WHEN 'Idle time' THEN '3' 
                                   WHEN 'Opportune Maintenance' THEN '4' 
                                   WHEN 'Planned External Downtime' THEN '5' 
                                   WHEN 'Planned External Downtime' THEN '5' 
                                   WHEN 'Planned Maintenance Mechanical' THEN '5' 
                                   WHEN 'Planned Maintenance Electrical' THEN '6' 
                                   WHEN 'Unplanned Downtime Operational' THEN '7' 
                                   WHEN 'Unplanned Downtime Mechanical' THEN '8' 
                                   WHEN 'Unplanned Downtime Electrical' THEN '9' 
                                   ELSE '99' 
                                 END AS [Rank] 
                    FROM   oeeevent oe 
                           INNER JOIN rsbizware.dbo.oeeconfigevent ce 
                                   ON oe.loeeconfigeventid = 
                                      ce.loeeconfigeventid 
                           INNER JOIN rsbizware.dbo.oeelovcodeval rs 
                                   ON oe.sstartval = rs.sdescription 
                                      AND ce.loeeintrssqlid = rs.loeelovcodeid 
                           INNER JOIN rsbizware.dbo.oeestateconfig mms 
                                   ON rs.lmachinestate = mms.loeestateconfigid 
                    WHERE  qq.tstart BETWEEN oe.tstart AND oe.tend 
                           AND oe.spartid = 'Ore-Hoist' 
                    ORDER  BY qq.tstart ASC) OA 
WHERE  ( tstart >= @ReportingStart 
         AND tstart < @ReportingEnd ) 
       AND sworkcelldescription = 'Hoisting' 
       AND ddurationseconds > 5 
ORDER  BY tstart ASC 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM