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
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