如何更快地使以下SQL代码

时间:2015-05-28 22:38:17

标签: sql oracle

我正在尝试编写一个Oracle SQL Developer代码,该代码将汇总每位患者的选定临床事件,这些事件发生在入院当天,即他们入院之日(beg_effective_dt_tm),用于某些服务和护理单位。它需要很多很多小时才能运行......

如果有人有任何想法,想要了解如何加速代码。提前谢谢

SELECT ENCNTR_ID, ROUND(BEG_EFFECTIVE_DT_TM,'DDD'), max(ventilator), max(O2FLOW), max(FIO2), min(PAO2), min(SPO2) FROM (
SELECT UNIQUE
EN.ENCNTR_ID,
ELH.BEG_EFFECTIVE_DT_TM,
max (CASE WHEN ce.event_cd = '43504571' THEN '1' ELSE '0' END) OVER (PARTITION BY elh.encntr_id, round(performed_dt_tm,'DDD')) AS ventilator,
max (case when ce.event_cd = '111123182' then ce.result_val else '' end) over (partition by elh.encntr_id, round(performed_dt_tm,'DDD'))  as O2FLOW,
max (case when ce.event_cd = '14611845' then ce.result_val else '' end) over (partition by elh.encntr_id, round(performed_dt_tm,'DDD'))  as FIO2,
min (case when ce.event_cd = '6758629' then ce.result_val else '' end) over (partition by elh.encntr_id, round(performed_dt_tm,'DDD'))  as PAO2,
MIN (Case When Ce.Event_cd = '51517102' Then Ce.Result_val Else '' End) OVER (PARTITION BY elh.ENCNTR_ID, ROUND(PERFORMED_DT_TM,'DDD'))  As SPO2

FROM
ENCOUNTER EN
join CLINICAL_EVENT CE on EN.ENCNTR_ID = CE.ENCNTR_ID
right JOIN ENCNTR_LOC_HIST ELH ON EN.ENCNTR_ID = EN.ENCNTR_ID

WHERE 
CE.EVENT_CD IN ()


AND elh.MED_SERVICE_CD IN (29279,29280, 32177, 32182, 29243, 32187, 32201, 32202, 10795466, 32209) 
AND elh.LOC_NURSE_UNIT_CD IN (95785229, 95785312, 95785470, 95785577, 95780475, 95780595, 95781786 ,95782289, 95784755, 95784860, 95785057, 95785139,95785789, 95785888, 95786353, 95786453)
AND CE.PERFORMED_DT_TM >= '01-JAN-2011' 
AND CE.PERFORMED_DT_TM < '01-JAN-2012' 
AND ROUND(CE.PERFORMED_DT_TM, 'DDD') = ROUND(ELH.BEG_EFFECTIVE_DT_TM, 'DDD') 

)
GROUP BY ENCNTR_ID, ROUND(BEG_EFFECTIVE_DT_TM,'DDD')
ORDER BY ENCNTR_ID, ROUND(BEG_EFFECTIVE_DT_TM,'DDD')

1 个答案:

答案 0 :(得分:3)

distinctgroup by一起使用始终是可疑的。就像做分析函数然后聚合一样。我想你想要这样的事情:

SELECT EN.ENCNTR_ID, ROUND(ELH.BEG_EFFECTIVE_DT_TM, 'DDD') as BEG_EFFECTIVE_DT_TM,
       max(CASE WHEN ce.event_cd = '43504571' THEN 1 ELSE 0 END) AS ventilator,
       max(case when ce.event_cd = '111123182' then ce.result_val end) as O2FLOW,
       max(case when ce.event_cd = '14611845' then ce.result_val end) as FIO2,
       min(case when ce.event_cd = '6758629' then ce.result_val end)  as PAO2,
       MIN(Case When Ce.Event_cd = '51517102' Then Ce.Result_val As SPO2
FROM ENCOUNTER EN join
     CLINICAL_EVENT CE
     on EN.ENCNTR_ID = CE.ENCNTR_ID JOIN
     ENCNTR_LOC_HIST ELH
     ON EN.ENCNTR_ID = EN.ENCNTR_ID
WHERE CE.EVENT_CD IN () AND
      elh.MED_SERVICE_CD IN (29279,29280, 32177, 32182, 29243, 32187, 32201, 32202, 10795466, 32209) AND
      elh.LOC_NURSE_UNIT_CD IN (95785229, 95785312, 95785470, 95785577, 95780475, 95780595, 95781786 ,95782289, 95784755, 95784860, 95785057, 95785139,95785789, 95785888, 95786353, 95786453) AND
      CE.PERFORMED_DT_TM >= '01-JAN-2011' AND
      CE.PERFORMED_DT_TM < '01-JAN-2012' AND
      ROUND(CE.PERFORMED_DT_TM, 'DDD') = ROUND(ELH.BEG_EFFECTIVE_DT_TM, 'DDD') 
GROUP BY ENCNTR_ID, BEG_EFFECTIVE_DT_TM
ORDER BY ENCNTR_ID, BEG_EFFECTIVE_DT_TM;

一些注意事项:

  • RIGHT JOIN是不必要的,因为WHERE子句将其转换为内连接。
  • 分析功能全部转为简单聚合。
  • WHERE子句中的最后一个条件应该是ON子句。
  • ELSE语句的CASE是不必要的,因为聚合函数会自动忽略NULL个值。