最近在做一个ibatis+extjs的项目,在做的过程中遇到一个问题,一条查询语句如下(原语句)

select ib.* ,(select sum(it.fconsignamount) from icstockbillentry it where it.finterid=ib.finterid) ysje,(select sum(t.skje) from collectionrecords t where t.djbh=ib.fbillno) skje1,from icstockbill ib left join customer cu on cu.id = ib.fsupplyid  

where 1=1虽然只是一条简单的语句但是查询效率极慢大约5-6秒,因为select列表中有一个聚合函数,我想一般做oa项目这样的求和是不可避免的吧。这个查询的执行过程大概是这样的,Extjs在前台传递一个start和count(count代表查询记录数,start代表开始数),然后经过action处理(不解释),调用

queryForList(statementName, qDto, intStart.intValue(), end.intValue());我们在控制台查看语句发现分页是这样的between 1 and 20,去掉分页之后反而速度更快,原来oracle的分页是嵌套查询的,between……and反而更慢。在网上查了一下有的说需要改源码,将hibernate方式注入等等,但是我做的是公司项目,框架都是封装了改动是十分困难的,然后在ibatis中的查询语句修改一下(如下)

select * from ( select rownum rn,ib.* ,(select sum(it.fconsignamount) from icstockbillentry it where it.finterid=ib.finterid) ysje, from icstockbill ib left join customer cu on cu.id = ib.fsupplyid where 1=1 and rownum<![CDATA[ <= ]]>to_number(#count#)) where rn<![CDATA[ > ]]>to_number(#start#)  ,然后在action中取出start和count,if(start!=0){  count= start+count};

最后直接调用ibatis的queryForList(statementName, new oo());oo里面存放了start和count的属性,只用这三步查询效率会大大提高