zoukankan      html  css  js  c++  java
  • 用Java向数据库中插入大量数据时的优化

    使用jdbc向数据库插入100000条记录,分别使用statement,PreparedStatement,及PreparedStatement+批处理3种方式进行测试: 

    [java] view plain copy
     
    1. public void exec(Connection conn){      
    2.   try {      
    3.    //开始时间     
    4.    Long beginTime = System.currentTimeMillis();      
    5.    //设置手动提交      
    6.    conn.setAutoCommit(false);     
    7.          
    8.    Statement st = conn.createStatement();      
    9.     
    10.    for(int i=0;i<100000;i++){      
    11.       String sql="insert into t1(id) values ("+i+")";      
    12.       st.executeUpdate(sql);       
    13.    }      
    14.         
    15.    //结束时间     
    16.    Long endTime = System.currentTimeMillis();      
    17.     
    18.    System.out.println("st:"+(endTime-beginTime)/1000+"秒");//计算时间      
    19.     
    20.    st.close();      
    21.    conn.close();      
    22.   } catch (SQLException e) {      
    23.    e.printStackTrace();      
    24.   }       
    25. }     


    //2.使用PreparedStatement对象 

    [java] view plain copy
     
    1. public void exec2(Connection conn){      
    2.   try {      
    3.    Long beginTime = System.currentTimeMillis();      
    4.    conn.setAutoCommit(false);//手动提交      
    5.    PreparedStatement pst = conn.prepareStatement("insert into t1(id) values (?)");      
    6.    for(int i=0;i<100000;i++){      
    7.     pst.setInt(1, i);      
    8.     pst.execute();         
    9.    }      
    10.    conn.commit();      
    11.    Long endTime = System.currentTimeMillis();      
    12.    System.out.println("pst:"+(endTime-beginTime)/1000+"秒");//计算时间      
    13.    pst.close();      
    14.    conn.close();      
    15.   } catch (SQLException e) {      
    16.    e.printStackTrace();      
    17.   }      
    18.     
    19. }     



    //3.使用PreparedStatement + 批处理 

    [java] view plain copy
     
    1. public void exec3(Connection conn){      
    2.   try {      
    3.    conn.setAutoCommit(false);      
    4.    Long beginTime = System.currentTimeMillis();      
    5.    //构造预处理statement     
    6.    PreparedStatement pst = conn.prepareStatement("insert into t1(id) values (?)");      
    7.    //1万次循环     
    8.    for(int i=1;i<=100000;i++){         
    9.     pst.setInt(1, i);      
    10.     pst.addBatch();      
    11.     //每1000次提交一次     
    12.     if(i%1000==0){//可以设置不同的大小;如50,100,500,1000等等      
    13.      pst.executeBatch();      
    14.      conn.commit();      
    15.      pst.clearBatch();      
    16.     }      
    17.    }     
    18.    Long endTime = System.currentTimeMillis();      
    19.    System.out.println("pst+batch:"+(endTime-beginTime)/1000+"秒");      
    20.    pst.close();      
    21.    conn.close();      
    22.   } catch (SQLException e) {      
    23.    e.printStackTrace();      
    24.   }      
    25. }     


    在Oracle 10g中测试,结果: 
    1.使用statement  耗时142秒; 
    2.使用PreparedStatement 耗时56秒; 
    3.使用PreparedStatement + 批处理耗时: 
       a.50条插入一次,耗时5秒; 
        b.100条插入一次,耗时2秒; 
        c.1000条以上插入一次,耗时1秒; 
    通过以上可以得出结论,在使用jdbc大批量插入数据时,明显使用第三种方式(PreparedStatement + 批处理)性能更优。

    -------------------------------------------------------------------------------------------------------------------------------

    1. 普通方式处理大量数据的insert时,处理速度相当慢。   
    2. */   
    3. PreparedStatement ps = null;   
    4. //循环10000次   
    5. for(int i = 0; i < 100000; i++) {   
    6.     ps = con.prepareStatement(sql);   
    7.     ps.executeUpdate();   
    8. }  
      1. 方法二:通过addBatch()的方式,将数据缓存在对象里面,通过最后执行executeBatch();方法提交,因此速度会快很多!   
      2. */   
      3. PreparedStatement ps = con.prepareStatement(sql);   
      4. for(int i = 0; i < 100000; i++) {   
      5.     ps.setString(1, "1");   
      6.     ps.setString(2, "2");   
      7.     ps.addBatch();   
      8. }   
      9. ps.executeBatch(); 
  • 相关阅读:
    一、链式
    C#链式编程
    五、通过密码访问API
    四.二、控制台
    一、bootstrap-datepicker
    悔录
    四、IDS4建立Authorization server和Client
    三、IDS4建立authorization server
    一、前端请求后台方式
    【2019-10-09】思想是为了克服不懂而存在的
  • 原文地址:https://www.cnblogs.com/shizhijie/p/8534887.html
Copyright © 2011-2022 走看看