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

关于rewriteBatchedStatements的源码分析

在之前的优惠券兑换码需求中,涉及批量写入问题,其中有一个关键的连接配置参数非常重要——rewriteBatchedStatements,当该值配置为true时,Statement将可能对批量插入sql进行重写。

何谓重写?原来提交的批量执行语句(100条)如下:

INSERT INTO dh_redeem_code (code, status, coupon_id, batch_id) VALUES ('1','0',100,1);
INSERT INTO dh_redeem_code (code, status, coupon_id, batch_id) VALUES ('2','0',100,1);
...
INSERT INTO dh_redeem_code (code, status, coupon_id, batch_id) VALUES ('100','0',100,1);

重写之后的sql语句(1条)如下:

INSERT INTO dh_redeem_code (code, status, coupon_id, batch_id) VALUES 
('1','0',100,1),
('2','0',100,1),
...
('100','0',100,1);

本文将从源码层面来对该参数的作用进行分析。笔者采用的mysql数据库驱动依赖库为mysql-connector-java:8.0.30。

首先,我们来看一段ClientPreparedStatement.executeBatchInternal()方法的代码,这是数据库驱动内部执行批量sql的方法。

if (!this.batchHasPlainStatements && this.rewriteBatchedStatements.getValue()) {if (getQueryInfo().isRewritableWithMultiValuesClause()) {return executeBatchWithMultiValuesClause(batchTimeout);}if (!this.batchHasPlainStatements && this.query.getBatchedArgs() != null&& this.query.getBatchedArgs().size() > 3 /* cost of option setting rt-wise */) {return executePreparedBatchAsMultiStatement(batchTimeout);}
}

关于batchHasPlainStatements属性,如果批处理使用 Statement.addBatch(String) 显示添加sql语句,则该属性值为true,否则为false。原文如下:

/*** Does the batch (if any) contain "plain" statements added by Statement.addBatch(String)?* If so, we can't re-write it to use multi-value or multi-queries.*/
protected boolean batchHasPlainStatements = false;

第一行代码的意思:如果没有使用 Statement.addBatch(String) 显示添加sql语句,并且rewriteBatchedStatements参数值为true,则有可能对批量语句进行重写。

继续往下看,isRewritableWithMultiValuesClause这个属性值为true,则执行executeBatchWithMultiValuesClause方法,该方法实现对sql语句的重写。

对于isRewritableWithMultiValuesClause属性的赋值,QueryInfo类的构造函数中

public QueryInfo(String sql, Session session, String encoding) {// Check if the statement has potential to be rewritten as a multi-values clause statement, i.e., if it is an INSERT or REPLACE statement and// 'rewriteBatchedStatements' is enabled.boolean rewritableAsMultiValues = (isInsert || isReplace) && rewriteBatchedStatements;...        //这里有很长很长很长一段代码this.isRewritableWithMultiValuesClause = rewritableAsMultiValues;
}

从第4行代码看出重写的必要条件:rewriteBatchedStatements值为true,而且必须是插入操作或者REPLACE操作。

再来看看sql语句重写函数executeBatchWithMultiValuesClause(batchTimeout)代码:

batchedStatement = /* FIXME -if we ever care about folks proxying our JdbcConnection */prepareBatchedInsertSQL(locallyScopedConn, numValuesPerBatch);timeoutTask = startQueryTimer(batchedStatement, batchTimeout);numberToExecuteAsMultiValue = numBatchedArgs < numValuesPerBatch ? numBatchedArgs : numBatchedArgs / numValuesPerBatch;int numberArgsToExecute = numberToExecuteAsMultiValue * numValuesPerBatch;for (int i = 0; i < numberArgsToExecute; i++) {if (i != 0 && i % numValuesPerBatch == 0) {try {updateCountRunningTotal += batchedStatement.executeLargeUpdate();} catch (SQLException ex) {sqlEx = handleExceptionForBatch(batchCounter - 1, numValuesPerBatch, updateCounts, ex);}getBatchedGeneratedKeys(batchedStatement);batchedStatement.clearParameters();batchedParamIndex = 1;}batchedParamIndex = setOneBatchedParameterSet(batchedStatement, batchedParamIndex, this.query.getBatchedArgs().get(batchCounter++));
}try {updateCountRunningTotal += batchedStatement.executeLargeUpdate();
} catch (SQLException ex) {sqlEx = handleExceptionForBatch(batchCounter - 1, numValuesPerBatch, updateCounts, ex);
}getBatchedGeneratedKeys(batchedStatement);

上述代码第一行将根据批量sql数量和插入的字段数量,构建带有占位符的sql。随后的语句遍历批量sql,使用实际的插入参数值替换占位符,从而形成一条可执行的批量插入语句。

自此,关于rewriteBatchedStatements的源码分析结束,如果需要数据库驱动重写批量插入,只需要正常使用MybatisPlus的saveBatch方法,并在数据库连接中加上rewriteBatchedStatements=true的配置信息即可。

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

相关文章:

  • 自动化神器 Playwright 的 Web 自动化测试解决方案
  • docker filebeat 将日志多级目录和多维json数据日志同步到es
  • 【机器学习】模型参数优化工具:Optuna使用分步指南(附XGB/LGBM调优代码)
  • webview全屏处理,即插即用
  • 实录分享 | 央企大数据平台架构发展趋势与应用场景的介绍
  • UE5 将类修改目录
  • GPT实战系列-ChatGLM3管理工具的API接口
  • Python 列表、元组、字典区别
  • [足式机器人]Part2 Dr. CAN学习笔记 - Ch03 傅里叶级数与变换
  • 你想使用域名访问一个ip的网页,你应该怎么办呢?
  • SAP存放状态的几个常用表
  • AUTO SEG-LOSS SEARCHING METRIC SURROGATES FOR SEMANTIC SEGMENTATION
  • openssl3.2 - 官方demo学习 - 索引贴
  • textarea文本框根据输入内容自动适应高度
  • 【JAVA基础--计算机网络】--TCP三次握手+四次挥手
  • 最新靠谱可用的-Mac-环境下-FFmpeg-环境搭建
  • 【漏洞复现】Hikvision SPON IP网络对讲广播系统存在命令执行漏洞CVE-2023-6895
  • 微软为Windows内置记事本应用开发AI功能;2024年15个 AI 语音生成器
  • 【C++进阶06】红黑树图文详解及C++模拟实现红黑树
  • 2023年最严重的10起0Day漏洞攻击事件
  • Linux之Iptables简易应用
  • 树状结构查询 - 华为OD统一考试
  • 版本控制系统教程
  • Java多线程并发篇----第十篇
  • 模型\视图一般步骤:为什么经常要用“选择模型”QItemSelectionModel?
  • C#,愚弄数(Hoax Number)的计算方法与源代码
  • c JPEG编码,此程序没有处现MCU中亮度分量的排序
  • 前端规范扩展
  • 【AI视野·今日NLP 自然语言处理论文速览 第七十二期】Mon, 8 Jan 2024
  • RT-Thread基于AT32单片机的CAN应用