MyBatis Oracle 批量插入数据
创始人
2025-01-15 08:33:59
0次

MyBatis Oracle 批量插入数据

  • 1.需求描述
  • 2.实现方案
    • 2.1 循环 insert 插入
    • 2.2 insert all 插入
    • 2.3 insert union all 插入
  • 3.分析总结

系统:Win10
JDK:1.8.0_351
IDEA:2022.3.3

1.需求描述

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

2.实现方案

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

2.1 循环 insert 插入

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

java 代码

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

mapper 接口

int insert(Region region); 

xml 语句

     insert into DATA_REGION(id, name, pid)     values (#{id}, #{name}, #{pid})  

这里测试执行了 3 次,平均耗时:15160 ms
在这里插入图片描述

2.2 insert all 插入

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

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 代码

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

mapper 接口

int insertAll(List list); 

xml 语句

     insert all              into DATA_REGION(id, name, pid) VALUES (#{item.id},#{item.name},#{item.pid})          select 1 from dual  

这里测试执行了 3 次,平均耗时:8171 ms
在这里插入图片描述

2.3 insert union all 插入

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

java 代码

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

mapper 接口

int batchInsert(List list); 

xml 语句

     insert into DATA_REGION(id, name, pid)              (         select #{item.id},#{item.name},#{item.pid} from dual         )       

这里测试执行了 3 次,平均耗时:2619 ms
在这里插入图片描述

3.分析总结

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

#循环 insert 插入insert all 插入insert union all 插入
平均耗时15.16s8.17s2.62s

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

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

相关内容

热门资讯

最新开挂!微信微乐小程序游戏辅... 最新开挂!微信微乐小程序游戏辅助脚本,微信小程序辅助器免费版在进入微信微乐小程序游戏辅助脚本软件靠谱...
最新透视挂!微乐小程序免费黑科... 最新透视挂!微乐小程序免费黑科技,微信小程序微乐房间有技巧吗1、微乐小程序免费黑科技透视辅助软件激活...
最新辅助!微信微乐小程序修改器... 最新辅助!微信微乐小程序修改器,微乐陕西小程序破解器下载1、微信微乐小程序修改器脚本辅助下载、微信微...
最新透视挂!微信微乐小程序有挂... 最新透视挂!微信微乐小程序有挂黑科技,微乐家乡麻将辅助1、下载好微信微乐小程序有挂黑科技脚本下载之后...
最新开挂!微乐家乡麻将自建房辅... 最新开挂!微乐家乡麻将自建房辅助app,微乐小程序自建房辅助下载1、打开软件启动之后找到中间准星的标...
最新透视挂!微乐小程序辅助工具... 最新透视挂!微乐小程序辅助工具,如何下载微乐自建房黑科技入口1、首先打开微乐小程序辅助工具辅助器下载...
最新开挂!微乐家乡麻将自建房开... 最新开挂!微乐家乡麻将自建房开挂,微信小程序微乐房间的挂多少钱一、微乐家乡麻将自建房开挂可以开透视的...
最新辅助!微信微乐辅助免费,微... 最新辅助!微信微乐辅助免费,微乐小程序自建房透视免费1、游戏颠覆性的策略玩法,独创攻略技巧玩法,可透...
最新透视挂!小程序微乐游戏辅助... 最新透视挂!小程序微乐游戏辅助器,微乐自建房脚本入口1、下载好小程序微乐游戏辅助器透视辅助下载之后点...
最新辅助!微信微乐小程序辅助器... 最新辅助!微信微乐小程序辅助器免费安装,微乐小程序免费黑科技在线下载1、金币登录送、破产送、升级送、...