当前位置: 首页 > news >正文

Jdbc批处理功能和MybatisPlus

文章目录

  • 1. 序言
  • 2. JDBC批处理功能和rewriteBatchedStatements
  • 3. JDBC批量插入的测试
  • 4. MybatisPlus#ServiceImpl.saveBatch()
  • 5. 结语:如果对大家有帮助,请点赞支持。如果有问题随时在评论中指出,感谢。

1. 序言

MybatisPlus的ServiceImpl类中提供了批处理方法saveBatch,用来批量插入数据,速度要比”一个一个”插入更快。而这个方法的底层依赖了JDBC数据库驱动的批处理功能。
本文先介绍JDBC数据库驱动的批处理功能,然后对"JDBC批量插入"进行性能测试,从而说明saveBatch快的原因,最后说明MybatisPlus ServiceImpl.saveBatch的最佳使用方式(其实就一句话)。

2. JDBC批处理功能和rewriteBatchedStatements

  • JDBC批处理功能是指:将多条SQL语句打包起来,一次性发给数据库服务器,服务器执行后将结果返回给客户端。相对于单条SQL逐条发送,批处理功能可以减少网络传输开销。JDBC的Statement和PreparedStatement都支持批处理,下面测试基于PreparedStatement。
    • PreparedStatement.addBatch()用来添加一条SQL到列表中,但是不会提交给服务器。
    • PreparedStatement.executeBatch()将列表中的所有SQL提交给服务器,并获得执行结果。
  • rewriteBatchedStatements:假如数据库URL连接设置了参数rewriteBatchedStatements=true,那么执行executeBatch()时,多条INSERT语句就会被重写为一条INSERT语句,再发送给服务器,使得插入速度更快。默认值为false。

3. JDBC批量插入的测试

  1. 配置:MySQL驱动 8.3.0,两台电脑:PC1运行MySQL服务器,PC2作为客户端运行以下测试程序,PC1和PC2在同一局域网。
  2. 表结构:
	CREATE TABLE `user` (`id` bigint NOT NULL,`username` varchar(255) NOT NULL,`age` int NOT NULL,PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
  1. 获取数据库连接代码
	private Connection getConnection() throws Exception {String url = "jdbc:mysql://192.168.1.9:3306/jdbc";String username = "root";String password = "root";return DriverManager.getConnection(url, username, password);}
  1. 逐条插入:耗时171172 ms
    @Testpublic void testSingleInsert() throws Exception {long start = System.currentTimeMillis();// 逐条SQL语句发送Connection conn = getConnection();PreparedStatement psmt = conn.prepareStatement("INSERT INTO user VALUES (?, ?, ?)");for (int i=1; i<=10000; i++) {psmt.setLong(1, i);psmt.setString(2, "user_" + i);psmt.setInt(3, 17);psmt.execute();}long end = System.currentTimeMillis();
//       171172 msSystem.out.println("cost = " + (end-start) + " ms");}
  1. 批量插入:耗时116244 ms,使用批处理减少了网络开销,变快了。
    @Testpublic void testBatchInsert() throws Exception {long start = System.currentTimeMillis();// 批量SQL发送Connection conn = getConnection();PreparedStatement psmt = conn.prepareStatement("INSERT INTO user VALUES (?, ?, ?)");for (int i=1; i<=10000; i++) {psmt.setLong(1, i);psmt.setString(2, "user_" + i);psmt.setInt(3, 17);psmt.addBatch();if (i % 1000 == 0) {psmt.executeBatch();}}long end = System.currentTimeMillis();
//        116244 msSystem.out.println("cost = " + (end-start) + " ms");}

注意:经过测试,如果MySQL服务器和客户端运行在同一台PC,"批量插入"和上一个"逐条插入"花费时间差不多,因为网络传输消耗的时间太小。

  1. 批量插入 + 手动开启事务:耗时68922 ms
    上一个"批量插入"的例子,服务器执行每一条INSERT SQL都会自动开启和提交事务。而"批量插入 + 手动开启事务"只开启和提交了一次事务,所以速度会更快。
    @Testpublic void testBatchInsertWithTransaction() throws Exception {long start = System.currentTimeMillis();// 批量SQL发送Connection conn = getConnection();conn.setAutoCommit(false);PreparedStatement psmt = conn.prepareStatement("INSERT INTO user VALUES (?, ?, ?)");for (int i=1; i<=10000; i++) {psmt.setLong(1, i);psmt.setString(2, "user_" + i);psmt.setInt(3, 17);psmt.addBatch();if (i % 1000 == 0) {psmt.executeBatch();}}conn.commit();long end = System.currentTimeMillis();//cost = 68922 msSystem.out.println("cost = " + (end-start) + " ms");}
  1. 批量插入 + 手动开启事务 + rewriteBatchedStatements:耗时1532 ms
    private Connection getConnection() throws Exception {// 添加参数rewriteBatchedStatements=trueString url = "jdbc:mysql://192.168.1.9:3306/jdbc?rewriteBatchedStatements=true";String username = "root";String password = "root";return DriverManager.getConnection(url, username, password);}@Testpublic void testBatchInsertWithTransactionAndRewrite() throws Exception {long start = System.currentTimeMillis();// 批量SQL发送,Connection conn = getConnection();conn.setAutoCommit(false);PreparedStatement psmt = conn.prepareStatement("INSERT INTO user VALUES (?, ?, ?)");for (int i=1; i<=10000; i++) {psmt.setLong(1, i);psmt.setString(2, "user_" + i);psmt.setInt(3, 17);psmt.addBatch();if (i % 1000 == 0) {psmt.executeBatch();}}conn.commit();long end = System.currentTimeMillis();//cost = 1532 msSystem.out.println("cost = " + (end-start) + " ms");}

可见"批量插入 + 手动开启事务 + rewriteBatchedStatements"速度最快。

4. MybatisPlus#ServiceImpl.saveBatch()

MybatisPlus 3.5.8
ServiceImpl.saveBatch()
可以看到ServiceImpl.saveBatch是开启了事务的,因此性能 = “批量插入 + 手动开启事务”。
所以通常我们会在JDBC数据库连接的后面加上rewriteBatchedStatements=true,变成"批量插入 + 手动开启事务 + rewriteBatchedStatements",达到最佳性能。
因此MybatisPlus批量插入的最佳方式是:saveBatch + rewriteBatchedStatements。

5. 结语:如果对大家有帮助,请点赞支持。如果有问题随时在评论中指出,感谢。

http://www.lryc.cn/news/478133.html

相关文章:

  • 对于相对速度的重新理解
  • Scala的属性访问权限(一)默认访问权限
  • 【算法】(Python)贪心算法
  • 条件logistic回归原理及案例分析
  • redis7学习笔记
  • 重学Android:自定义View基础(一)
  • 前端好用的网站分享——CSS(持续更新中)
  • 华为HarmonyOS借助AR引擎帮助应用实现虚拟与现实交互的能力3-获取设备位姿
  • qt QColorDialog详解
  • 【测试小白--如何写好测试用例--测试用例编写的方法+结合常见登录模块为实例--保姆级教学】
  • 真题--数组循环题目
  • 【Linux系列】在Linux下安装微信
  • 还在使用ElementUI不如试一试DaisyUI,DaisyUI: Tailwind CSS 的高效组件库,
  • 高光谱激光雷达遥感团队成员白杰博士获全国激光雷达优博论文奖
  • 24年配置CUDA12.4,Pytorch2.5.1,CUDAnn9.5运行环境
  • 基于springboot得高校评教教师工作量管理系统设计与实现
  • Rust 力扣 - 1456. 定长子串中元音的最大数目
  • 【Golang】validator库的使用
  • 【AI日记】24.11.06 我对投资的一点浅见
  • 2024江苏省网络建设与运维省赛Linux(一)系统安装
  • 详解Python面向对象程序设计
  • JS保留两位小数
  • ClickHouse集成Mysql表引擎跨服务器读表说明
  • 【AI构思渲染】别眨眼!这些图片立马变效果图!
  • 多特征变量序列预测(10)基于麻雀优化算法的CEEMDAN-SSA-Transformer-BiLSTM预测模型
  • 算法学习(十)—— 字符串
  • 「Mac畅玩鸿蒙与硬件16」鸿蒙UI组件篇6 - List 和 Grid 组件展示数据列表
  • masm汇编字符输入小写转大写演示
  • 防火墙|WAF|漏洞|网络安全
  • 继承机制深度解析:从基础到进阶的完整指南