这个是原本的sql,直接执行的时候在执行计划中可以看到3张表是直接全表扫描了。
我分别将2个条件进行了拆分,每次只执行一个结果如下:
我又将sql写法改成了union all ,也是走索引的,所以我想问问大家,是不是使用了or一定就不会走索引了?
记账表的索引列我也放在下面的截图了:
具体的sql如下,证件号我去掉了:
select b.olrptno c1, b.caseno c2, b.oolrptno c3, case when a.clmstate = '70' then '0' else '1' end c4, c.systemname c5, (select h.codename from ldcode h where h.codetype = 'clmstate' and trim(a.clmstate) = trim(h.code)) c6, (select auditidea from llclaimuwmain where clmno = a.rgtno) c7, a.rgtdate c8, a.endcasedate c9, b.customername c10, nvl(c.StandbyFlag3, 0) c11, (select nvl(sum(lb.realpay), 0) from llclaimdetail lb where lb.caseno = b.caseno and lb.givetype = '0') c12, a.grpcontno c13, (select count(1) from llcasereceipt la where la.caseno = b.caseno) c14, b.remark c15, a.togetherflag c16, b.condoleflag c17, case when c.makedate is null then a.rgtdate else c.makedate end c18, decode(a.riskcode, '211701', 'Y', 'N') c19 from llregister a inner join llcase b on a.rgtno = b.rgtno left join llclaimprt c on c.rptno = b.oolrptno inner join lcinsured d on d.contno = b.contno where 1 = 1 and ((d.name = '张松' and d.idtype = '0' and d.idno = '') or b.customerno in (select lg.customerno from lcinsuredrelated lg inner join lcinsured jj on jj.contno = lg.contno inner join ldperson lh on lg.maincustomerno = lh.customerno where floor(MONTHS_BETWEEN(sysdate, jj.birthday) / 12) < 18 and lh.name = '张松' and lh.idtype = '0' and lh.idno = ''))
第一个or查询语句左边有三个键的联合索引,但是右边没有。在SQL当中,or语句如果有一个字段没有索引的话是不会走索引的。所以尽量避免使用or