黑猴子的家:JDBC -> 批量处理 jdbc 语句提高处理速度

1、概念

(1)当需要成批插入或者更新记录时。可以采用Java的批量更新机制,这一机制允许多条语句一次性提交给数据库批量处理。通常情况下比单独提交处理更有效率

(2)JDBC的批量处理语句包括下面三个方法: addBatch(String):添加需要批量处理的SQL语句或是参数; executeBatch():执行批量处理语句; clearBatch():清空缓存的数据

(3)通常我们会遇到两种批量执行SQL语句的情况: 多条SQL语句的批量处理; insert 、update 一个SQL语句的批量传参 insert values √ 常用

2、没有使用批处理

1)检查 mysql 插入速度

select count(*) from admin;
truncate table admin;
mysql 数据库检查 插入速度

2)code

package com.yinggu.demo5;

import java.sql.Connection;
import java.sql.PreparedStatement;
import org.junit.Test;
import com.yinggu.utils.JDBCUtils;

 * @author:黑猴子的家
 * @博客 :https://www.jianshu.com/u/37fd8e2dff4c
 *
 * 批处理 情况:
 *  一、多条sql语句的批量处理 适用于 sql语句各不一样,使用Statement命令对象执行 
 *  二、一条sql语句的批量传参
 * 适用于sql语句一样,仅仅是参数不同,使用PreparedStatement命令对象指向
 * 相关API 
 *  addBatch:添加命令到批处理包
 *  executeBatch:提交批处理包
 *  clearBatch:清空批处理包
 * 
 * 好处: 减少和数据库服务器连接的次数,减少执行的次数,提高效率

public class TestBatch {
      //没有使用批处理
      //select count(*) from admin;
      //truncate table admin;
      //mysql 数据库检查 插入速度
      @SuppressWarnings("unused")
      @Test
      public void testNoBatch() throws Exception {
            // 1.获取连接
            Connection connection = JDBCUtils.getConnection();
            // 2.访问数据库
            PreparedStatement statement = connection.prepareStatement(
                 "insert into admin values(null,?,?)");
            for (int i = 1; i <= 50000; i++) {
                  statement.setString(1, "小花" + i);
                  statement.setString(2, "0000");
                  int update = statement.executeUpdate();
            }
            // 3.关闭
            JDBCUtils.closeConnection(null, statement, connection);
      }
}

3、使用了批处理

1) 检查 mysql 插入速度

select count(*) from admin;
truncate table admin;
mysql 数据库检查 插入速度

2)批量处理注意事项

(1)使用批处理,首先选对驱动包

5.1.37是可以的,5.1.7 是不可以的
jar包不同,批处理支持的程度不同

(2)开启批处理

url=jdbc:mysql:///orcl?rewriteBatchedStatements=true&useServerPrepStmts=false

3)code

// 使用了批处理
// (1)使用批处理,首先选对驱动包,5.1.37是可以的,5.1.7 是不可以的
//  jar包不同,批处理支持的程度不同
// (2)开启批处理
//  #url=jdbc:mysql:///orcl?rewriteBatchedStatements=true&useServerPrepStmts=false
@Test
public void testUseBatch() throws Exception {
   // 1.获取连接
   Connection connection = JDBCUtils.getConnection();
   // 2.访问数据库
   PreparedStatement statement = connection.prepareStatement(
             "insert into admin values(null,?,?)");
   for (int i = 1; i < 50000; i++) {
      statement.setString(1, "小花" + i);
      statement.setString(2, "8888");
      // 批处理
      statement.addBatch();// 将要执行的sql语句添加到批处理包(装篮子的操作)
      if (i % 1000 == 0) {
         statement.executeBatch();// 提交批处理的命令们(上楼运篮子的操作)
         statement.clearBatch();// 清空批处理包(卸篮子的操作)
      } else if (i > 49000) {
         statement.executeBatch();// 提交批处理的命令们(上楼运篮子的操作)
         statement.clearBatch();// 清空批处理包(卸篮子的操作)
      }
   }
   // 3.关闭
   JDBCUtils.closeConnection(null, statement, connection);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值