当前位置:Gxlcms > 数据库问题 > jdbc批量插入实现大批量数据快速插入

jdbc批量插入实现大批量数据快速插入

时间:2021-07-01 10:21:17 帮助过:3人阅读

      requires a log flush to disk for every insert. To disable autocommit during your import operation, surround it with

      SET autocommit and COMMIT statements:

      SET autocommit=0;
     ... SQL import statements ...
     COMMIT;

    第一次,正是因为没有setAutoCommit(false);那么对于每一条insert语句,都会产生一条log写入磁盘,所以虽然设置了批量插入,但其效果就像单条插入一样,导致插入速度十分缓慢。

    部分代码如下:

String sql = "insert into table *****";
con.setAutoCommit(false);
ps = con.prepareStatement(sql);
for(int i=1; i<65536; i++){
    ps.addBatch();
    // 1w条记录插入一次
    if (i % 10000 == 0){
         ps.executeBatch();
         con.commit();
     }
}
// 最后插入不足1w条的数据
ps.executeBatch();
con.commit();

 

jdbc批量插入实现大批量数据快速插入

标签:

人气教程排行