MyBatis Oracle 批量插入数据

MyBatis Oracle 批量插入数据

系统:Win10

JDK:1.8.0_351

IDEA:2022.3.3

1.需求描述

在一次项目中实施过程中,后台需要将地区数据插入到数据库,数据大概三千条左右,本来直接使用循环遍历插入的方式,结果居然需要十几秒的时间,感觉速度太慢了,所以便想办法提高插入数据的速度,经过多番资料查询和验证,结果选定了一款实现方式,特此记录下来。

2.实现方案

数据:3208 条

需求:将数据插入到 Oracle 数据库

2.1 循环 insert 插入

最基础的就是使用 for 循环遍历 list 数据,一个个进行插入

java 代码

java 复制代码
...
 //循环插入
 StopWatch started = new StopWatch();
 started.start();//开始计时
 for (Region region : regions) {
     regionMapper.insert(region);
 }
 started.stop();//停止计时
 System.out.println("插入" + list.size() + "条数据耗时: " + started.getTime());
...

mapper 接口

java 复制代码
int insert(Region region);

xml 语句

xml 复制代码
<insert id="insert">
    insert into DATA_REGION(id, name, pid)
    values (#{id}, #{name}, #{pid})
</insert>

这里测试执行了 3 次,平均耗时:15160 ms

2.2 insert all 插入

这里主要使用的 Oracle 的 INSERT ALL 语法

sql 复制代码
INSERT ALL
    INTO table_name(col1,col2,col3) VALUES(val1,val2, val3)
    INTO table_name(col1,col2,col3) VALUES(val4,val5, val6)
    INTO table_name(col1,col2,col3) VALUES(val7,val8, val9)
Subquery;

该方法用来批量向数据库中插入数据所用的

java 代码

java 复制代码
...
//insert all 插入
StopWatch started = new StopWatch();
started.start();//开始计时
regionMapper.insertAll(regions);
started.stop();//停止计时
System.out.println("插入" + list.size() + "条数据耗时: " + started.getTime());
...

mapper 接口

java 复制代码
int insertAll(List<Region> list);

xml 语句

xml 复制代码
<insert id="insertAll">
    insert all
    <foreach collection="list" item="item" index="index">
        into DATA_REGION(id, name, pid) VALUES (#{item.id},#{item.name},#{item.pid})
    </foreach>
    select 1 from dual
</insert>

这里测试执行了 3 次,平均耗时:8171 ms

2.3 insert union all 插入

这里是使用 union all 先将各个对象的值查询到一起再批量插入到表中

java 代码

java 复制代码
...
//batchInsert
StopWatch started = new StopWatch();
started.start();//开始计时
regionMapper.batchInsert(regions);
started.stop();//停止计时
System.out.println("插入" + list.size() + "条数据耗时: " + started.getTime());
...

mapper 接口

java 复制代码
int batchInsert(List<Region> list);

xml 语句

xml 复制代码
<insert id="batchInsert">
    insert into DATA_REGION(id, name, pid)
    <foreach collection="list" item="item" index="index" separator="union all">
        (
        select #{item.id},#{item.name},#{item.pid} from dual
        )
    </foreach>
</insert>

这里测试执行了 3 次,平均耗时:2619 ms

3.分析总结

根据上面的三次测试,我们可以得出如下表格

# 循环 insert 插入 insert all 插入 insert union all 插入
平均耗时 15.16s 8.17s 2.62s

所以我最后决定采用第三种方法,不过我这里数据量只有 3000 多条,如果数据量更大,可能要再测试看看。

后续看看还有没有更好的实现方案,如果找到我再添加到文章末尾。

相关推荐
小白学大数据几秒前
Java 爬虫对百科词条分类信息的抓取与处理
java·开发语言·爬虫
古城小栈2 分钟前
Spring Boot 数据持久化:MyBatis-Plus 分库分表实战指南
spring boot·后端·mybatis
无名-CODING26 分钟前
SQL 注入指南
sql·mybatis
Coder_Boy_1 小时前
Spring 核心思想与企业级最佳特性(实践级)事务相关
java·数据库·spring
历程里程碑1 小时前
hot 206
java·开发语言·数据结构·c++·python·算法·排序算法
Coder_Boy_1 小时前
Java+Proteus仿真Arduino控制LED问题排查全记录(含交互过程)
java·人工智能·python
一 乐1 小时前
校园实验室|基于springboot + vue校园实验室管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端
Lisonseekpan2 小时前
Spring Boot Email 邮件发送完全指南
java·spring boot·后端·log4j
sheji34162 小时前
【开题答辩全过程】以 基于Springboot的体检中心信息管理系统设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
天河归来2 小时前
本地windows环境升级dify到1.11.1版本
java·spring boot·docker