基于JdbcTemplate的批量插入操作的讨论

在MySql数据库中,经常需要批量插入数据,如何能够更高效地插入数据,在这里讨论了三种方法:
1. 直接利用for循环进行insert;
2. 利用JdbcTemplate中的batchUpdate来进行批量更新;
3. 利用mysql拼接语句来进行一次性更新。
测试的代码如下:
1. 直接利用for循环进行insert;

@Rollback(false)
 @Test
 public void testInsert(){
  Long beginTime = System.currentTimeMillis();
  String app_id = "1";
  String[] domainIdArr = new String[cnt];
  for(int i=1;i<=cnt;i++){
   domainIdArr[i-1] = Objects.toString(i);
  }
  String sql ="delete from tbl_route where app_id = ?";
     jdbcTemplate.update(sql, new Object[]{app_id});
     //1、插入appId需要绑定的域名
     String sql2 = "insert into tbl_route (app_id, domain_id, create_time) values (?,?,?)";
     for(int i=0;i<domainIdArr.length;i++){
      String domain_id = domainIdArr[i];
      Long create_time = new Date().getTime();
      jdbcTemplate.update(sql2, new Object[]{Integer.parseInt(app_id), Integer.parseInt(domain_id), create_time});
     }
     Long endTime = System.currentTimeMillis();
     Long spendTime = endTime-beginTime;
     System.out.println("花费时间:"+spendTime);
 }

2. 利用JdbcTemplate中的batchUpdate来进行批量更新;

  @Rollback(false)
 @Test
 public void testBatchUpdate(){
  Long beginTime = System.currentTimeMillis();
  String app_id = "1";
  String[] domainIdArr = new String[cnt];
  for(int i=1;i<=cnt;i++){
   domainIdArr[i-1] = Objects.toString(i);
  }
  String sql ="delete from tbl_route where app_id = ?";
     jdbcTemplate.update(sql, new Object[]{app_id});
  String sql2 = "insert into tbl_route (app_id, domain_id, create_time) values (?,?,?)";
  List<Object[]> batchArgs = new ArrayList<Object[]>();
  for(int i=0;i<domainIdArr.length;i++){
   String domain_id = domainIdArr[i];
      Long create_time = new Date().getTime();
      batchArgs.add(new Object[]{Integer.parseInt(app_id), Integer.parseInt(domain_id), create_time});
  }
  jdbcTemplate.batchUpdate(sql2, batchArgs);
  Long endTime = System.currentTimeMillis();
     Long spendTime = endTime-beginTime;
     System.out.println("花费时间:"+spendTime);
 }

3. 利用mysql拼接语句来进行一次性更新。

 @Rollback(false)
 @Test
 public void testThirdUpdate(){
  Long beginTime = System.currentTimeMillis();
  String app_id = "1";
  String[] domainIdArr = new String[cnt];
  for(int i=1;i<=cnt;i++){
   domainIdArr[i-1] = Objects.toString(i);
  }
  String sql ="delete from tbl_route where app_id = ?";
     jdbcTemplate.update(sql, new Object[]{app_id});
  String sql2 = "insert into tbl_route (app_id, domain_id, create_time) values ";
  List<Object[]> batchArgs = new ArrayList<Object[]>();
  StringBuffer sqlBuffer = new StringBuffer();
  for(int i=0;i<domainIdArr.length;i++){
   String domain_id = domainIdArr[i];
      Long create_time = new Date().getTime();
      if(i != domainIdArr.length-1){
          sqlBuffer.append("(").append(app_id).append(",").append(domain_id).append(",").append(create_time).append(")").append(",");
      }else{
       sqlBuffer.append("(").append(app_id).append(",").append(domain_id).append(",").append(create_time).append(")");
      }
  }
  String sql3 = sql2.concat(sqlBuffer.toString());
  jdbcTemplate.execute(sql3);
  Long endTime = System.currentTimeMillis();
     Long spendTime = endTime-beginTime;
     System.out.println("花费时间:"+spendTime);
 }

上述的三种方法花费的时间依次为:
5246
5387
515
如果单纯的只是执行插入操作的话,用拼接语句的效果有了一个量级的提升。

你可能感兴趣的:(mysql)