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

时间:2016-09-18 05:08:54

标签: sql sql-server

我试图通过减少内部查询来提高查询的性能。

我需要第一个,因为它返回ReasonTUM。

但是,我想使用ReasonTUM的一个案例,以便它也返回一个数字。

我怎样才能做到这一点?我尝试在原始查询中引用mms.sMachineStateName,但它没有检测到列。

我能想到的另一种方法是使用另一个子查询,但由于连接,这需要大约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

1 个答案:

答案 0 :(得分:3)

使用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