JDBC大数据插入性能优化对比
来源:互联网 发布:win7找不到网络计算机 编辑:程序博客网 时间:2024/05/21 06:11
这个对我来说确实收获不小,一次性插入上万条数据,确实好使。
在Oracle 10g中测试,结果:
1、使用statement耗时142秒;
2、使用PreparedStatement耗时56秒;
3、使用PreparedStatement + 批处理耗时:
a.50条插入一次,耗时5秒;
b.100条插入一次,耗时2秒;
c.1000条以上插入一次,耗时1秒;
使用jdbc向数据库插入100000条记录,分别使用statement,PreparedStatement,及PreparedStatement+批处理3种方式进行测试:
//1、使用statement插入100000条记录 public void exec(Connection conn){ try { Long beginTime = System.currentTimeMillis(); conn.setAutoCommit(false);//设置手动提交 Statement st = conn.createStatement(); for(int i=0;i<100000;i++){ String sql="insert into t1(id) values ("+i+")"; st.executeUpdate(sql); } Long endTime = System.currentTimeMillis(); System.out.println("st:"+(endTime-beginTime)/1000+"秒");//计算时间 st.close(); conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //2、使用PreparedStatement对象 public void exec2(Connection conn){ try { Long beginTime = System.currentTimeMillis(); conn.setAutoCommit(false);//手动提交 PreparedStatement pst = conn.prepareStatement("insert into t1(id) values (?)"); for(int i=0;i<100000;i++){ pst.setInt(1, i); pst.execute(); } conn.commit(); Long endTime = System.currentTimeMillis(); System.out.println("pst:"+(endTime-beginTime)/1000+"秒");//计算时间 pst.close(); conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //3、使用PreparedStatement + 批处理 public void exec3(Connection conn){ try { conn.setAutoCommit(false); Long beginTime = System.currentTimeMillis(); PreparedStatement pst = conn.prepareStatement("insert into t1(id) values (?)"); for(int i=1;i<=100000;i++){ pst.setInt(1, i); pst.addBatch(); if(i00==0){//可以设置不同的大小;如50,100,500,1000等等 pst.executeBatch(); conn.commit(); pst.clearBatch(); } } Long endTime = System.currentTimeMillis(); System.out.println("pst+batch:"+(endTime-beginTime)/1000+"秒"); pst.close(); conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
在Oracle 10g中测试,结果:
1、使用statement耗时142秒;
2、使用PreparedStatement耗时56秒;
3、使用PreparedStatement + 批处理耗时:
a.50条插入一次,耗时5秒;
b.100条插入一次,耗时2秒;
c.1000条以上插入一次,耗时1秒;
通过以上可以得出结论,在使用jdbc大批量插入数据时,明显使用第三种方式(PreparedStatement + 批处理)性能更优。
欢迎加入Java交流群:398918539
0 0
- JDBC大数据插入性能优化对比
- jdbc插入数据优化
- JDBC各种插入数据的速度对比
- 大数据应用之HBase数据插入性能优化之多线程并行插入测试案例
- sql优化:SQL Server与Oracle性能对比(插入100w条数据时)
- mysql大数据插入效率对比
- jdbc插入大量数据时优化处理-最近遇到的解决我一大难题
- JDBC批量插入数据的性能测试
- 优化JDBC性能的三大技巧
- 大数据应用之HBase数据插入性能优化实测教程
- 大数据应用之HBase数据插入性能优化实测教程
- 【Java/JDBC.ORM】 jdbc插入大量数据时优化处理
- 【Java/JDBC.ORM】 jdbc插入大量数据时优化处理
- hibernate与jdbc在插入数据上的速度对比
- sql大数据插入之性能改善
- Sqlite3插入大量数据性能优化
- .NET 批量插入数据,先查后插,性能优化
- Android批量插入数据性能优化
- ArcGIS教程:了解多元分类
- Mac常用快捷键
- android ListView ListSelector 不起作用(被覆盖)
- kvm虚拟化之virt-install
- WeiXin Share{微信分享}
- JDBC大数据插入性能优化对比
- js绑定事件-(坑)
- PayPal高级工程总监:读完这100篇论文 就能成大数据高手
- 应用程序无法正常启动0xc0150002 解决方案
- u-boot启动画面制作
- Spring-MVC并发访问的线程安全性问题
- Log4j本地试验成功
- 如何编写更省电的Android应用
- ARM版本及系列