select sum(case when c.id_Dep_Pi_Af = '60877de521997e2901a929a1' and c.sd_Apply_Ap_Cd in ('1', '5') then 1 else 0 end) as depPiInNum, SUM(DECODE(c.ID_DEP_PI_BEF, '60877de521997e2901a929a1', 1, 0)) as depPiOutNum from Hi_Vis_Med a left join Hi_Vis_Med_Ip b on a.id_Vismed = b.id_Vismed left join Hi_Vis_Med_Dep_Pi_Changeap c on b.id_Vismed = c.id_Vismed where a.sd_Vistp_Cd = '114' and a.delete_Flag = 0 and c.sd_Apply_Ap_Cd in ('1', '2', '5', '6'); sql优化
时间: 2024-04-05 10:33:15 浏览: 60
可以尝试以下优化方式:
1. 创建索引:可以在Hi_Vis_Med、Hi_Vis_Med_Ip、Hi_Vis_Med_Dep_Pi_Changeap表中分别创建以下列的索引:id_Vismed、sd_Vistp_Cd、delete_Flag、id_Dep_Pi_Af、sd_Apply_Ap_Cd、ID_DEP_PI_BEF等。
2. 使用EXISTS代替LEFT JOIN:可以将LEFT JOIN改为EXISTS,如下所示:
select sum(case when c.id_Dep_Pi_Af = '60877de521997e2901a929a1' and c.sd_Apply_Ap_Cd in ('1', '5') then 1 else 0 end) as depPiInNum,
SUM(DECODE(c.ID_DEP_PI_BEF, '60877de521997e2901a929a1', 1, 0)) as depPiOutNum
from Hi_Vis_Med a
where a.sd_Vistp_Cd = '114' and a.delete_Flag = 0
and exists (
select 1 from Hi_Vis_Med_Ip b, Hi_Vis_Med_Dep_Pi_Changeap c
where a.id_Vismed = b.id_Vismed and b.id_Vismed = c.id_Vismed and c.sd_Apply_Ap_Cd in ('1', '2', '5', '6')
);
3. 将DECODE函数转成CASE语句:可以将DECODE函数转成CASE语句,如下所示:
select sum(case when c.id_Dep_Pi_Af = '60877de521997e2901a929a1' and c.sd_Apply_Ap_Cd in ('1', '5') then 1 else 0 end) as depPiInNum,
SUM(CASE WHEN c.ID_DEP_PI_BEF = '60877de521997e2901a929a1' THEN 1 ELSE 0 END) as depPiOutNum
from Hi_Vis_Med a
where a.sd_Vistp_Cd = '114' and a.delete_Flag = 0
and exists (
select 1 from Hi_Vis_Med_Ip b, Hi_Vis_Med_Dep_Pi_Changeap c
where a.id_Vismed = b.id_Vismed and b.id_Vismed = c.id_Vismed and c.sd_Apply_Ap_Cd in ('1', '2', '5', '6')
);
阅读全文