当前位置:网站首页>Four implementations of batch insert: have you really got it?
Four implementations of batch insert: have you really got it?
2022-08-01 03:06:00 【Joeyz efforts】

本文收录与专栏:《JDBC》专栏
专栏目的是解释JDBC的关键点,与各位一路同行,会持续输出,欢迎免费订阅!!

一、批量插入
Bulk insert is批量执行SQL语句
当需要成批插入或者更新记录时,可以采用Java的批量更新机制,这一机制允许多条语句一次性提交给数据库批量处理.通常情况下比单独提交处理更有效率
JDBCBatch processing statement include the following三个方法:
●addBatch(String):添加需要批量处理的SQL语句或是参数
●executeBatch():执行批量处理语句
●clearBatch():清空缓存的数据
Normally we will meet两种批量执行SQL语句的情况:
- 多条SQL语句的批量处理
- 一个SQL语句的批量传参
二、举例
举例:向数据表中插入20000条数据
数据库中提供一个goods表:创建如下:
CREATE TABLE goods(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(20)
);
实现方式一
使用Statement
Connection conn = JDBCUtils.getConnection();
Statement st = conn.createStatement();
for(int i = 1;i <= 20000;i++){
String sql = "insert into goods(name) values('name_' + "+ i +")";
st.executeUpdate(sql);
}
实现方式二
使用PreparedStatement
long start = System.currentTimeMillis();
Connection conn = JDBCUtils.getConnection();
String sql = "insert into goods(name)values(?)";
PreparedStatement ps = conn.prepareStatement(sql);
for(int i = 1;i <= 20000;i++){
ps.setString(1, "name_" + i);
ps.executeUpdate();
}
long end = System.currentTimeMillis();
System.out.println("花费的时间为:" + (end - start));//82340
JDBCUtils.closeResource(conn, ps);
实现方式三
- 使用
addBatch() / executeBatch() / clearBatch() - mysql服务器默认是关闭批处理的,我们需要通过一个参数,让mysql开启批处理的支持
?rewriteBatchedStatements=true 写在配置文件的url后面 - 使用更新的mysql 驱动:
mysql-connector-java-5.1.37-bin.jar
@Test
public void testInsert3() throws Exception{
long start = System.currentTimeMillis();
Connection conn = JDBCUtils.getConnection();
String sql = "insert into goods(name)values(?)";
PreparedStatement ps = conn.prepareStatement(sql);
for(int i = 1;i <= 1000000;i++){
ps.setString(1, "name_" + i);
//1.“攒”sql
ps.addBatch();
if(i % 500 == 0){
//2.执行
ps.executeBatch();
//3.清空
ps.clearBatch();
}
}
long end = System.currentTimeMillis();
System.out.println("花费的时间为:" + (end - start));//20000条:625 //1000000条:14733
JDBCUtils.closeResource(conn, ps);
}
实现方式四
In the way on the basis of three operation
使用Connection 的 setAutoCommit(false) / commit()
@Test
public void testInsert4() throws Exception{
long start = System.currentTimeMillis();
Connection conn = JDBCUtils.getConnection();
//1.设置为不自动提交数据
conn.setAutoCommit(false);
String sql = "insert into goods(name)values(?)";
PreparedStatement ps = conn.prepareStatement(sql);
for(int i = 1;i <= 1000000;i++){
ps.setString(1, "name_" + i);
//1.“攒”sql
ps.addBatch();
if(i % 500 == 0){
//2.执行
ps.executeBatch();
//3.清空
ps.clearBatch();
}
}
//2.提交数据
conn.commit();
long end = System.currentTimeMillis();
System.out.println("花费的时间为:" + (end - start));//1000000条:4978
JDBCUtils.closeResource(conn, ps);
}
The four way always forget,浅浅总结下,Hope will be useful~
边栏推荐
- Parse the bootargs from the device tree (dtb format data)
- Handwritten binary search tree and test
- 如何下载Keil包
- 开源项目站点必备&交流区功能
- RTL8762DK RTC (5)
- 【入门教程】Rollup模块打包器整合
- This map drawing tool is amazing, I recommend it~~
- [Message Notification] How about using the official account template message?
- "Youth Pie 2": The new boyfriend stepped on two boats, and the relationship between Lin Miaomiao and Qian Sanyi warmed up
- Flutter “Hello world“ 程代码
猜你喜欢

Basic implementation of vector

Flink deploys and submits jobs

How to download the Keil package

"Youth Pie 2": The new boyfriend stepped on two boats, and the relationship between Lin Miaomiao and Qian Sanyi warmed up

树莓派 的 arm 版的 gcc 安装 和环境变量的配置

IDEA does not recognize the module (there is no blue square in the lower right corner of the module)

二舅

Game Security 03: A Simple Explanation of Buffer Overflow Attacks

ARM cross compilation

被 CSDN,伤透了心
随机推荐
Chinese version of Pylint inspection rules
【消息通知】用公众号模板消息怎么样?
pdb药物综合数据库
MYSQL transactions
Euler system (euleros): upgrade Mysql
MYSQL Keyword Explain Analysis
Google Earth Engine - Error resolution of Error: Image.clipToBoundsAndScale, argument 'input': Invalid type
Take you to experience a type programming practice
MySQL modifies SQL statements to optimize performance
By CSDN, torn
opencv 缩小放大用哪种插值更好??
[Data analysis] Based on matlab GUI student achievement management system [including Matlab source code 1981]
leetcode6133. 分组的最大数量(中等)
Introduction to machine learning how to?
【 Make YOLO Great Again 】 YOLOv1 v7 full range with large parsing (Neck)
MYSQL logical architecture
MYSQL Index Analysis
leetcode6132. 使数组中所有元素都等于零(简单,周赛)
手写二叉查找树及测试
RTL8762DK RTC (5)