首页 分享 JDBC的批处理学习rewriteBatchedStatements=true

JDBC的批处理学习rewriteBatchedStatements=true

来源:花匠小妙招 时间:2024-12-15 11:51

最新推荐文章于 2024-06-27 17:17:14 发布

weixin_34126557 于 2018-05-26 15:04:00 发布

如果在不添加批处理指令的情况下,mysql默认是不使用批处理操作,如果在url尾部添加rewriteBatchedStatements=true

可以使当前连接 使用批处理操作

创建数据库表结构

create table test( id int primary key auto_increment, name varchar(20), age int, sex varchar(10));

未使用批出的url链是

jdbc:mysql://localhost:3306/mysql1

测试源码是:

package JDBCDemo; import java.sql.Connection; import java.sql.PreparedStatement; import org.junit.Test; import JDBCUtils.jdbcUtils; public class JDBCdemo5 { /* * 批处理实验 在批处理情况下 速度更快 * 在url之后添加rewriteBatchedStatements=true 开启批出操作 */ @Test public void DoMore() throws Exception{ /* * 依然是四大对象 获取连接 */ Connection connection =jdbcUtils.getConnection(); String sql="insert into test values(?,?,?,?)"; PreparedStatement preparedStatement=connection.prepareStatement(sql); for (int i = 1; i < 20002; i++) { preparedStatement.setInt(1, i); preparedStatement.setString(2, "o153014841"+i); preparedStatement.setInt(3, i); preparedStatement.setString(4, i%2==0? "男":"女"); preparedStatement.addBatch(); } long start=System.currentTimeMillis(); preparedStatement.executeBatch(); long end=System.currentTimeMillis(); System.out.println(end-start); } }

测试结果为

由于在测试时原本只想测试2000个数据 但是一不小心测试为2万条数据 造成时间比较长 这是测试的疏忽,

然后测试使用的jdbcUtils只是获取connection连接 不具备其他功能 然后测试可以使用批出指令的时间为

前后结果相差巨大 批处理速度快很多

转载于:https://www.cnblogs.com/ad-zhou/p/9092941.html

相关知识

【JDBC实战】水果库存系统 [代码优化]
JDBC与MyBatis:数据库访问技术的变迁【后端 15】
深度学习实战:AlexNet实现花图像分类
通过Excel表格和批处理脚本批量新建并按顺序命名文件夹
@RequestParam(required = true)的误区
使用CNN或resnet,分别在flower5,flower17,flower102数据集上实现花朵识别分类
数据库连接复用 MultipleActiveResultSets=true
推测性解码:实现 Whisper 推理速度提升两倍
jmockdata拓展1.支持LocalDateTime等,2.支持lombok的@Accessors(chain=true)
Educoder/头歌JAVA——Java Web:基于JSP的网上商城

网址: JDBC的批处理学习rewriteBatchedStatements=true https://www.huajiangbk.com/newsview1108839.html

所属分类:花卉
上一篇: 天气预测(CNN)
下一篇: 赏花预报

推荐分享