当前位置:网站首页>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~
边栏推荐
- 项目越写越大,我是这样做拆分的
- By Value or By Reference
- What practical projects can machine learning beginners learn?
- 【Make YOLO Great Again】YOLOv1-v7全系列大解析(Neck篇)
- Modify Postman installation path
- IDEA调试
- 《少年派2》:新男友竟脚踩两只船,林妙妙与钱三一感情回温
- How to get started with YOLO?How to implement your own training set?
- 预言机简介
- /usr/sbin/vmware-authdlauncher: error while loading shared libraries: libssl.so.1.0.2*Solution
猜你喜欢

《少年派2》:新男友竟脚踩两只船,林妙妙与钱三一感情回温

【Make YOLO Great Again】YOLOv1-v7全系列大解析(Neck篇)

Game Security 03: A Simple Explanation of Buffer Overflow Attacks

带wiringPi库在unbutu 编译 并且在树莓派运行

Completely closed Chrome updated and in the top right corner of the tip

Four ways the Metaverse is changing the way humans work

how to edit the table of contents of an epub ebook

移动端页面秒开优化总结

Raspberry pie arm version of GCC installed configuration and environment variables

IDEA无法识别module(module右下角没有蓝色小方块)
随机推荐
This map drawing tool is amazing, I recommend it~~
【uniCloud】云对象的应用与提升
787. 归并排序
黑客到底可以厉害到什么程度?
更换树莓派内核
second uncle
【元胞自动机】基于matlab界面聚合元胞自动机模拟【含Matlab源码 2004期】
元宇宙改变人类工作模式的四种方式
2. # 代码注释
MySQL modifies SQL statements to optimize performance
情人节浪漫3D照片墙【附源码】
The kernel of the decompression process steps
Ordinary users cannot access HGFS directory
MYSQL Classic Interview Questions
/usr/sbin/vmware-authdlauncher: error while loading shared libraries: libssl.so.1.0.2*Solution
Euler system (euleros): upgrade Mysql
从设备树(dtb格式数据)中解析出bootargs
Basic usage concepts of vim
test
【Make YOLO Great Again】YOLOv1-v7全系列大解析(Neck篇)