sql优化Demo

select sum(sl0000) from xstfxps2 where
dhao00 in (
select dhao00 from xstfxps1 where trunc(ywrq00)=trunc(sysdate)
and khdm00=’500000003913’);

通常来说,如果语句能够避免子查询的使用,就尽量不用子查询。因为子查询的开销是相当昂贵的。
改写后的语句如下:
select sum(sl0000)
from xstfxps2 a,(select dhao00 from xstfxps1 where trunc(ywrq00)=trunc(sysdate)
and khdm00=’500000003913’) b
where a.dhao00=b.dhao00;

根据语句的查询情况,我们建立了如下的复合索引:
create index idx_xstfxps1_khdm00_ywrq00 on xstfxps1(khdm00,ywrq00) tablespace indx;   
为了使用索引,我们必须对原来的日期字段的条件进行一些调整。因为有个trunc()函数的存在,
语句将不会使用到索引。我们只要明白trunc(ywrq00)=trunc(sysdate)事实上等同于ywrq00大于
trunc(sysdate),小于trunc(sysdate+1)减去一秒,我们就有了比较好的办法来处理
这个条件。最终改写后的语句如下:
select sum(sl0000)
from xstfxps2 a, xstfxps1 b
where a.dhao00=b.dhao00 
and b.khdm00=’500000003913’
and b.ywrq00 between trunc(sysdate)
and trunc(sysdate)+1-1/(24*60*60);
Execution Plan
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值