解决一个上周很郁闷的问题:
项目中有一个需求,一个表 数据量很大,有几百万条吧,保守估计,呵呵。页面上填入 每组 要显示的个数,租用时间(以秒为单位),逻辑就是我先分组,然后循环 每组里根据填入的个数查询出相应的数据,这些数据要写入到xml里,然后更新。
只用java+sql,就是那种最普通的方式,测试之后发现查询+更新需要18分钟完成100000条记录的操作,这根本不能满足需求。
还是请教一下别人哈,得到的答案是 在Oracle里写存储过程,我测了一下,确实快了不少。呵呵,就用它了。
先看 存储过程怎么写吧!
思路就是先分组,然后根据分组 每组再查询出用户要求的个数,放到一个临时表里,然后更新这些数据,最后查询出临时表里的数据返回一个游标。(注:临时表的创建放在command window 里执行)
create or replace procedure updatePro(returndataCur out IPINFO_PACKAGE.curList,curTime in number,endTime in number,dateTime in number,num in number) is
authId number;
--声明变量
pro VARCHAR2(32);
ipInfoId number;
info_ip number;
maskLen number;
portStart number;
portEnd number;
direct number;
rownumber number;
str varchar2(300);
cnt int;
Cursor groupCur is select auth_id,property from ip_info group by property,auth_id;
Cursor curListCur is select id,ip,mask_len,port_start,port_end,dir,property,auth_id from (select t.*,rownum rn from (select id,ip,mask_len,port_start,port_end,dir,property,auth_id from ip_info ip_info where USABLE_TIME_START + dateTime < = endTime and USABLE_TIME_END + dateTime > = endTime and NEXT_USE_TIME < curTime and property=pro and auth_id=authId order by id desc) t where rownum < = num ) s where rn> 0;
begin
--赋值
authId:=0;
pro:='m';
ipInfoId:=0;
info_ip:=0;
maskLen:=0;
portStart:=0;
portEnd:=0;
direct:=0;
rownumber:=0;
cnt:=0;
str:='delete from RETURNDATA';
select count(*) into cnt from all_tables where table_name='RETURNDATA';
--if cnt!=0 then
--execute immediate str;
--end if;
--此处是游标嵌套
open groupCur;
loop
fetch groupCur into authId,pro;
exit when groupCur%notfound;
--dbms_output.put_line(authId||pro);
open curListCur;
loop
fetch curListCur into ipInfoId,info_ip,maskLen,portStart,portEnd,direct,pro,authId;
exit when curListCur%notfound;
dbms_output.put_line(ipInfoId);
insert into RETURNDATA values(ipInfoId,info_ip,maskLen,portStart,portEnd,direct,pro,authId);
update ip_info set last_use_time=curTime,next_use_time=endTime where id=ipInfoId;
end loop;
commit;
close curListCur;
end loop;
close groupCur;
open returndataCur for select * from RETURNDATA;
end updatePro;
hibernate里调用存储过程
public List findAllIpInfoList(Long curTime,Long tempNum,Long dateTime,Integer number) {
ResultSet rs=null;
Session ses = this.getHibernateTemplate().getSessionFactory().openSession();
List ipInfoList=new ArrayList();
try{
Connection conn = ses.connection();
conn.setAutoCommit(false);
String proc="{Call updatepro(?,?,?,?,?)}";
CallableStatement st = conn.prepareCall(proc);
st.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR);//输出参数
st.setInt(2,Integer.valueOf(curTime.toString()));
st.setInt(3,Integer.valueOf(tempNum.toString()));
st.setInt(4,Integer.valueOf(dateTime.toString()));
st.setInt(5,number);
st.execute();
rs = (ResultSet) st.getObject(1);
while(rs.next())
{ IpInfo ipInfo=new IpInfo();
ipInfo.setIpinfoIpString(PublicClass.getJustIp(rs.getString(2)));
ipInfo.setMaskLin(rs.getInt(3));
ipInfo.setPortStart(rs.getInt(4));
ipInfo.setPortEnd(rs.getInt(5));
ipInfo.setDir(rs.getInt(6));
ipInfo.setProperty(rs.getString(7));
ipInfo.setAuthId(rs.getInt(8));
ipInfoList.add(ipInfo);
//System.out.println("<tr><td>" +rs.getLong(2)+"</td><td>"+ rs.getInt(3)+rs.getInt(4)+rs.getInt(5)+rs.getInt(6)+rs.getString(7)+rs.getInt(8)+"</td><td>");
}
conn.commit();
st.close();
conn.close();
ses.close();
}catch(Exception e){
e.printStackTrace();
}
return ipInfoList;
}