使用JDBC插入大量数据的性能测试

来源:互联网 发布:unity3d vr游戏开发 编辑:程序博客网 时间:2024/05/17 20:33
JDBCOracleSQL
Java代码 复制代码 收藏代码
  1. 2009-05-21     
  2. 使用JDBC插入大量数据的性能测试      
  3. 关键字: 性能测试      
  4.      
  5. 使用jdbc向数据库插入100000条记录,分别使用statement,PreparedStatement,及PreparedStatement+批处理3种方式进行测试:      
  6.      
  7. //1.使用statement插入100000条记录      
  8.      
  9.        
  10.      
  11. public void exec(Connection conn){      
  12.      
  13.   try {      
  14.      
  15.    Long beginTime = System.currentTimeMillis();      
  16.      
  17.    conn.setAutoCommit(false);//设置手动提交     
  18.      
  19.    Statement st = conn.createStatement();      
  20.      
  21.    for(int i=0;i<100000;i++){      
  22.      
  23.     String sql="insert into t1(id) values ("+i+")";      
  24.      
  25.     st.executeUpdate(sql);        
  26.      
  27.    }      
  28.      
  29.    Long endTime = System.currentTimeMillis();      
  30.      
  31.    System.out.println("st:"+(endTime-beginTime)/1000+"秒");//计算时间     
  32.      
  33.    st.close();      
  34.      
  35.    conn.close();      
  36.      
  37.   } catch (SQLException e) {      
  38.      
  39.    // TODO Auto-generated catch block      
  40.      
  41.    e.printStackTrace();      
  42.      
  43.   }        
  44.      
  45.  }      
  46.      
  47. //2.使用PreparedStatement对象      
  48.      
  49. public void exec2(Connection conn){      
  50.      
  51.   try {      
  52.      
  53.    Long beginTime = System.currentTimeMillis();      
  54.      
  55.    conn.setAutoCommit(false);//手动提交     
  56.      
  57.    PreparedStatement pst = conn.prepareStatement("insert into t1(id) values (?)");      
  58.      
  59.    for(int i=0;i<100000;i++){      
  60.      
  61.     pst.setInt(1, i);      
  62.      
  63.     pst.execute();          
  64.      
  65.    }      
  66.      
  67.    conn.commit();      
  68.      
  69.    Long endTime = System.currentTimeMillis();      
  70.      
  71.    System.out.println("pst:"+(endTime-beginTime)/1000+"秒");//计算时间     
  72.      
  73.    pst.close();      
  74.      
  75.    conn.close();      
  76.      
  77.   } catch (SQLException e) {      
  78.      
  79.    // TODO Auto-generated catch block      
  80.      
  81.    e.printStackTrace();      
  82.      
  83.   }      
  84.      
  85.  }      
  86.      
  87. //3.使用PreparedStatement + 批处理      
  88.      
  89. public void exec3(Connection conn){      
  90.      
  91.   try {      
  92.      
  93.    conn.setAutoCommit(false);      
  94.      
  95.    Long beginTime = System.currentTimeMillis();      
  96.      
  97.    PreparedStatement pst = conn.prepareStatement("insert into t1(id) values (?)");      
  98.      
  99.          
  100.      
  101.    for(int i=1;i<=100000;i++){          
  102.      
  103.     pst.setInt(1, i);      
  104.      
  105.     pst.addBatch();      
  106.      
  107.     if(i%1000==0){//可以设置不同的大小;如50,100,500,1000等等     
  108.      
  109.      pst.executeBatch();      
  110.      
  111.      conn.commit();      
  112.      
  113.      pst.clearBatch();      
  114.      
  115.     }      
  116.      
  117.    }      
  118.      
  119.    Long endTime = System.currentTimeMillis();      
  120.      
  121.    System.out.println("pst+batch:"+(endTime-beginTime)/1000+"秒");      
  122.      
  123.    pst.close();      
  124.      
  125.    conn.close();      
  126.      
  127.   } catch (SQLException e) {      
  128.      
  129.    // TODO Auto-generated catch block      
  130.      
  131.    e.printStackTrace();      
  132.      
  133.   }      
  134.      
  135.  }      
  136.      
  137. 在Oracle 10g中测试,结果:      
  138.      
  139. 1.使用statement耗时142秒;      
  140.      
  141. 2.使用PreparedStatement耗时56秒;      
  142.      
  143. 3.使用PreparedStatement + 批处理耗时:      
  144.      
  145. a.50条插入一次,耗时5秒;      
  146.      
  147. b.100条插入一次,耗时2秒;      
  148.      
  149. c.1000条以上插入一次,耗时1秒;      
  150.      
  151. 通过以上可以得出结论,在使用jdbc大批量插入数据时,明显使用第三种方式(PreparedStatement + 批处理)性能更优。   
http://javastudyeye.iteye.com/blog/835448
原创粉丝点击