下面一段sql语句查询效率极低,希望有大神指点迷津;
select 
                sum(case when d.acpt_ind='1' and (b.SYSTEM_CD is null or b.SYSTEM_CD='A') and (d.cuss_ind=1 or d.WebCKI_ind=1) and m.flt_seg_day_id is not null then 1
     when c.SYSTEM_CD is not null and d.acpt_ind='1' and a.airln_cd in ('MF','CZ','OQ') and d.cuss_ind=0 and d.WebCKI_ind=0 and c.cki_typ in ('C','W') and m.flt_seg_day_id is not null then 1
     else 0 end) as airpt_sec_bc,
sum(case when d.acpt_ind='1' and (b.SYSTEM_CD is null or b.SYSTEM_CD='A') and (d.cuss_ind=1 or d.WebCKI_ind=1) and m.flt_seg_day_id is not null then 1
     when c.SYSTEM_CD is not null and d.acpt_ind='1' and a.airln_cd in ('MF','CZ','OQ') and d.cuss_ind=0 and d.WebCKI_ind=0 and c.cki_typ in ('C','W') and m.flt_seg_day_id is not null then 1
     else 0 end) as airpt_sec_bag
from dss_pbidt.dcs_flt a
inner join 
(select flt_seg_day_id,pax_hist_id,acpt_ind,chkin_pid_nbr,cuss_ind,webcki_ind from dss_pdata.enr_check_in_event
union all
select flt_seg_day_id,pax_hist_id,acpt_ind,chkin_pid_nbr,cuss_ind,webcki_ind from dss_phist.enr_check_in_event_h2015 
union all
select flt_seg_day_id,pax_hist_id,acpt_ind,chkin_pid_nbr,cuss_ind,webcki_ind from dss_phist.enr_check_in_event_h2014)d
   on( a.flt_seg_day_id=d.flt_seg_day_id 
      and 
      d.acpt_ind=1
      )
   left outer join (select distinct trim(both from PID_NBR) as PID_NBR,SYSTEM_CD from dss_pdata.PRT_CUSS_PID where cz_ind=0)b
     on (trim(both from d.chkin_pid_nbr) = b.PID_NBR)
left outer join (select distinct trim(both from PID_NBR) as PID_NBR,SYSTEM_CD,cki_typ from dss_pdata.PRT_CUSS_PID where cz_ind=1)c
     on (trim(both from d.chkin_pid_nbr) = c.PID_NBR) 
left outer join 
(select distinct s.flt_seg_day_id as flt_seg_day_id,s.pax_hist_id as pax_hist_id from 
(select opr_dt,flt_seg_day_id,pax_hist_id,hist_actn_cd,opr_pid_nbr from dss_pdata.enr_pax_cki_history
union all
select opr_dt,flt_seg_day_id,pax_hist_id,hist_actn_cd,opr_pid_nbr from dss_phist.enr_pax_cki_history_h2015
union all
select opr_dt,flt_seg_day_id,pax_hist_id,hist_actn_cd,opr_pid_nbr from dss_phist.enr_pax_cki_history_h2014 ) s
where s.opr_dt>='2015-01-01' and s.opr_dt<'2015-02-01' and ((s.hist_actn_cd='BC' and s.opr_pid_nbr in (select distinct(pid_nbr) from
dss_pdata.prt_cuss_pid where system_cd in ('P','H') and cz_ind='0' )) or s.hist_actn_cd='BAG')  
)m
on
(
d.flt_seg_day_id=m.flt_seg_day_id
and
d.pax_hist_id=m.pax_hist_id
)
where a.lcl_dep_date between '20150101' and '20150201'