java一次查询几十万,几百万数据解决办法.docx_第1页
java一次查询几十万,几百万数据解决办法.docx_第2页
java一次查询几十万,几百万数据解决办法.docx_第3页
java一次查询几十万,几百万数据解决办法.docx_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

java一次性查询几十万,几百万数据解决办法 分类: java数据库2012-02-27 09:0816609人阅读评论(27)收藏举报java查询一次性查询几十万,几百万数据解决办法很早的时候写工具用的一个办法。 当时是用来把百万数据打包 成rar文件。所以用了个笨办法。 希望高手指导一下,有什么好方法没有啊1、先批量查出所有数据,例子中是一万条一批。2、在查出数据之后把每次的数据按一定规则存入本地文件。3、获取数据时,通过批次读取,获得大批量数据。此方法参见:/blog/static/175273432201191354043148/以下是查询数据库。按批次查询public static void getMonthDataList() ResultSet rs = null;Statement stat = null;Connection conn = null;List list = new ArrayList();try conn = createConnection();if(conn!=null)SimpleDateFormat sdf = new SimpleDateFormat(yyyy-MM-dd);SimpleDateFormat timesdf = new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);String nowDate = sdf.format(new Date();Config.lasttimetext = timesdf.format(new Date();String lastDate = sdf.format(CreateData.addDaysForDate(new Date(), 30);stat = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);int lastrow = 0;int datanum = 0;String countsql = SELECT count(a.id) FROM trip_special_flight a + where a.dpt_date = to_date(+nowDate+,yyyy-mm-dd) +and a.dpt_date +lastrow+ order by a.get_time desc;rs = stat.executeQuery(countsql);while (rs.next() datanum = rs.getInt(1);int onerun = 10000;int runnum = datanum%onerun=0?(datanum/onerun):(datanum/onerun)+1;for(int r =0;r= to_date(+nowDate+,yyyy-mm-dd) +and a.dpt_date +lastrow;stat.setMaxRows(onerun);stat.setFetchSize(1000);rs = stat.executeQuery(sql);String text = ;int i = 1;while (rs.next() text += rs.getString(2)+|+rs.getString(3)+|+rs.getDate(4)+|+rs.getString(5)+|+rs.getString(6)+|+rs.getString(7)+|+rs.getString(8)+|;if(i%1000=0)FileUtil.appendToFile(Config.tempdatafile, text);text = ;i+;if(text.length()10)FileUtil.appendToFile(Config.tempdatafile, text);lastrow+=onerun; catch (Exception e) e.printStackTrace(); finally closeAll(rs, stat, conn);-java一次性查询几十万,几百万数据解决办法存入临时文件之后,再用读取大量数据文件方法。设置缓存大小BUFFER_SIZE ,Config.tempdatafile是文件地址来源博客/blog/static/175273432201191354043148/package com.yjf.util;import java.io.File;import java.io.RandomAccessFile;import java.nio.MappedByteBuffer;import java.nio.channels.FileChannel;public class Test public static void main(String args) throws Exception final int BUFFER_SIZE = 0x300000; / 缓冲区为3MFile f = new File(Config.tempdatafile);/ 来源博客/blog/static/175273432201191354043148/int len = 0;Long start = System.currentTimeMillis();for (int z = 8; z 0; z-) MappedByteBuffer inputBuffer = new RandomAccessFile(f, r).getChannel().map(FileChannel.MapMode.READ_ONLY,f.length() * (z-1) / 8, f.length() * 1 / 8);byte dst = new byteBUFFER_SIZE;/ 每次读出3M的内容for (int offset = 0; offset = BUFFER_SIZE) for (int i = 0; i BUFFER_SIZE; i+)dsti = inputBuffer.get(offset + i); else for (int i = 0; i inputBuffer.capacity() - offset; i+)dsti = inputBuffer.get(offset + i);int length = (inputBuffer.capacity() % BUFFER_SIZE = 0) ? BUFFER_SIZE: inputBuffer.capacity() % BUFFER_SIZE;len += new String(dst, 0, length).length();System.out.println(new String(dst, 0, len

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论