oralce sql优化-包含多表not exists转left join,一个表查询重复数据

出现场景:

因上一篇 Oacle锁表,ORA-00054 出现锁表的原因是由于存储过程校验执行慢导致的,因此本篇对涉及到的存储过程中涉及到,多表关联查询是否存在导入信息,一个表中根据多个字段查询是否有重复信息,进行优化


优化分两部分

1、根据临时表关联业务表,校验未查询到相应的人员信息

原型:

update contributiontemp a
     set a.chectoutstatus = '02',
         a.chectoutinfo   = a.chectoutinfo || '第' || (a.expandfield1 + 1) ||
                            '行,未查询到相应的人员信息.' || chr(13) || chr(10)
   where a.ExpandField2 = '01' and  not exists (select distinct pnb.idno from PN_B_Staff pnb
         left join PN_B_StaffComPlan pns on pns.staffid=pnb.staffid
         where pns.PlanID =a.planid
         and upper(pnb.idno)=upper(a.idno)
         and pnb.name = a.name
         and pnb.idtype=decode(a.idtype,'身份证','01','居民身份证','01','军人证','02','其他','09','居民户口簿','10','驾驶证','11','军官证','02','士兵证','12','军官离退休证','13','中国护照','14','异常身份证','15','港澳通行证','20','台湾通行证','21','回乡证','16','外国护照','17','旅行证','18','居留证件','19','其它','09','')
         )
         and a.expandfield10 ='20210616152859459551' and a.chectoutstatus is null;

改造后:

update contributiontemp ctm
     set ctm.chectoutstatus = '02',
         ctm.chectoutinfo   = ctm.chectoutinfo || '第' || (ctm.expandfield1 + 1) ||
                            '行,未查询到相应的人员信息.' || chr(13) || chr(10)
 WHERE   exists(           
            select distinct a.contrilsid
            from contributiontemp a
                 left join 
                 (--1、取本计划所有的人员三证信息
                      SELECT distinct pnb.name, pnb.idno,pnb.idtype
                      FROM PN_B_StaffComPlan pns
                           left join PN_B_Staff pnb on pns.staffid=pnb.staffid
                      WHERE planid = '20210315111531000001'
                  ) pnstd
                  on upper(pnstd.idno)=upper(a.idno) and pnstd.name = a.name
                  and pnstd.idtype=decode(a.idtype,'身份证','01','居民身份证','01','军人证','02','其他','09','居民户口簿','10','驾驶证','11','军官证','02','士兵证','12','军官离退休证','13','中国护照','14','异常身份证','15','港澳通行证','20','台湾通行证','21','回乡证','16','外国护照','17','旅行证','18','居留证件','19','其它','09','')
             WHERE pnstd.idno is NULL
                   and a.ExpandField2 = '01'
                   and a.chectoutstatus is null
                   and a.expandfield10 ='20210616152859459551'
                   and ctm.contrilsid = a.contrilsid
             )

结论:经测试,在20w条数据上,改造后的sql,执行效率是原型sql的30倍以上,大大提高了效率;


2、在临时表中查询重复信息

原型:

update CONTRIBUTIONTEMP c
     set c.repeatcount    = '02',
         c.chectoutstatus = '02',
         c.chectoutinfo   = c.chectoutinfo || '第' ||(expandfield1+1) ||
                            '行,导入信息重复.' || chr(13) || chr(10)
   where c.ExpandField2 = '01'
     and c.expandfield10 = '20210616152859459551' and c.chectoutstatus is null
     and c.expandfield1 in
         (select distinct b.expandfield1
            from CONTRIBUTIONTEMP b
            left join CONTRIBUTIONTEMP d
              on b.expandfield10 = d.expandfield10
           where b.expandfield10 = '20210616152859459551'
                 and b.contrilsid <> d.contrilsid
                 and b.name = d.name
                 and upper(b.idno) = upper(d.idno)
                  );

改造后:

update CONTRIBUTIONTEMP c
     set c.repeatcount    = '02',
         c.chectoutstatus = '02',
         c.chectoutinfo   = c.chectoutinfo || '第' ||(expandfield1+1) ||
                            '行,导入信息重复.' || chr(13) || chr(10)
   where c.ExpandField2 = '01'
     and c.expandfield10 = '20210616165734152652' and c.chectoutstatus is null
     and exists (Select   a.contrilsid   From   CONTRIBUTIONTEMP   a   
           Where   Exists   (   
                 Select   b.expandfield1   from   CONTRIBUTIONTEMP  b 
                 where   b.expandfield10 = '20210616165734152652'
                          and b.contrilsid <> a.contrilsid
                          and b.name = a.name
                           and upper(b.idno) = upper(a.idno)
                           and b.subenterprisedeid = a.subenterprisedeid
                 )
                and a.contrilsid = c.contrilsid
                 )

结论:经测试,在20w条数据上,改造后的sql,执行效率是原型sql的30倍以上,大大提高了效率;


author:su1573
鄙人记录生活点滴,学习并分享,请多指教!!!
如需交流,请联系 sph1573@163.com,鄙人看到会及时回复

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ssy03092919

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值