最近工作中使用到批量删除,试了试网上的几种方法,下面三种方法都是插入2万条数据

使用oracle的insert all

特别注意:mysql默认接受sql的大小是1048576(1M),若数据量超过1M会报如异常错误者可以,进行分开处理,每次提交一定的数据到数据库,还可以通过调整MySQL安装目录下的my.ini文件中[mysqld]段的"max_allowed_packet = 1M"),增加max_allowed_packet 的容量。

mybatis的xml编写

使用到oracle批量插入的方法insert all

    <insert id="insertBatch3" useGeneratedKeys="false" parameterType="list">INSERT ALL<foreach collection ="list" item="applicationConsumables" index="index" separator =" ">INTO APPLICATION_CONSUMABLES_TB(BUSINESS_ID,GENERATE_DATE,CREATE_DATE,UPDATE_DATE,ARCHIVE_DATE,PATIENT_ID,AREA_ID,ORG_ID,ORG_NAME,WS98_00_909_35,WS98_00_909_36,ZJ99_JH_SBS,ZJ99_TY_CG_CS,ZJ99_TY_DB_CS,ZJ99_CG_JH_ZXS,ZJ99_CG_ZS,ZJ99_GYS_PS_WCZS,ZJ99_XPS_ZS)VALUES(#{applicationConsumables.businessId,jdbcType=VARCHAR},decode(#{applicationConsumables.generateDate,jdbcType=DATE},null,null,to_date(to_char(#{applicationConsumables.generateDate,jdbcType=DATE},'yyyy-MM-dd'),'yyyy-MM-dd')),decode(#{applicationConsumables.createDate,jdbcType=DATE},null,sysdate,to_date(#{applicationConsumables.createDate,jdbcType=DATE},'yyyy-MM-dd')),decode(#{applicationConsumables.updateDate,jdbcType=DATE},null,sysdate,to_date(to_char(#{applicationConsumables.updateDate,jdbcType=DATE},'yyyy-MM-dd'),'yyyy-MM-dd')),decode(#{applicationConsumables.archiveDate,jdbcType=DATE},null,null,to_date(to_char(#{applicationConsumables.archiveDate,jdbcType=DATE},'yyyy-MM-dd'),'yyyy-MM-dd')),#{applicationConsumables.patientId,jdbcType=VARCHAR},#{applicationConsumables.areaId,jdbcType=VARCHAR},#{applicationConsumables.orgId,jdbcType=VARCHAR},#{applicationConsumables.orgName,jdbcType=VARCHAR},#{applicationConsumables.ws980090935,jdbcType=VARCHAR},#{applicationConsumables.ws980090936,jdbcType=VARCHAR},#{applicationConsumables.zj99JhSbs,jdbcType=NUMERIC},#{applicationConsumables.zj99TyCgCs,jdbcType=NUMERIC},#{applicationConsumables.zj99TyDbCs,jdbcType=NUMERIC},#{applicationConsumables.zj99CgJhZxs,jdbcType=NUMERIC},#{applicationConsumables.zj99CgZs,jdbcType=NUMERIC},#{applicationConsumables.zj99GysPsWczs,jdbcType=NUMERIC},#{applicationConsumables.zj99XpsZs,jdbcType=NUMERIC})</foreach>SELECT 1 FROM DUAL</insert>

代码

把json数据转对象,一次提交插入500条

    @Overridepublic boolean insertBatch3(Map param) throws Exception {List list = JsonArrayToPojoList.jsonToList(param, ApplicationConsumablesPojo.class);int nums = 500; // 一次插入200条int times = (int)Math.ceil((float)list.size() / nums);// 插入次数try{ApplicationConsumablesPojo pojo= (ApplicationConsumablesPojo)list.get(0);applicationConsumablesMapper.deleteBatch(pojo.getGenerateDate(),pojo.getOrgId());for(int i=0; i < times; i++){if(i == times - 1) {applicationConsumablesMapper.insertBatch3(list.subList(i * nums, list.size()));}else {applicationConsumablesMapper.insertBatch3(list.subList(i * nums, (i+1) * nums));}}} catch(Exception e){return false;}return true;}

利用存储过程实现批量插入

mybatis的xml编写

useGeneratedKeys=“false”,表示不需要JDBC自动生成主键

     <insert id="insertBatch" useGeneratedKeys="false" parameterType="list">BEGIN<foreach collection ="list" item="applicationConsumables" index="index" separator =";">INSERT INTO APPLICATION_CONSUMABLES_TB(BUSINESS_ID,GENERATE_DATE,CREATE_DATE,UPDATE_DATE,ARCHIVE_DATE,PATIENT_ID,AREA_ID,ORG_ID,ORG_NAME,WS98_00_909_35,WS98_00_909_36,ZJ99_JH_SBS,ZJ99_TY_CG_CS,ZJ99_TY_DB_CS,ZJ99_CG_JH_ZXS,ZJ99_CG_ZS,ZJ99_GYS_PS_WCZS,ZJ99_XPS_ZS)VALUES(#{applicationConsumables.businessId,jdbcType=VARCHAR},decode(#{applicationConsumables.generateDate,jdbcType=DATE},null,null,to_date(to_char(#{applicationConsumables.generateDate,jdbcType=DATE},'yyyy-MM-dd'),'yyyy-MM-dd')),decode(#{applicationConsumables.createDate,jdbcType=DATE},null,sysdate,to_date(#{applicationConsumables.createDate,jdbcType=DATE},'yyyy-MM-dd')),decode(#{applicationConsumables.updateDate,jdbcType=DATE},null,sysdate,to_date(to_char(#{applicationConsumables.updateDate,jdbcType=DATE},'yyyy-MM-dd'),'yyyy-MM-dd')),decode(#{applicationConsumables.archiveDate,jdbcType=DATE},null,null,to_date(to_char(#{applicationConsumables.archiveDate,jdbcType=DATE},'yyyy-MM-dd'),'yyyy-MM-dd')),#{applicationConsumables.patientId,jdbcType=VARCHAR},#{applicationConsumables.areaId,jdbcType=VARCHAR},#{applicationConsumables.orgId,jdbcType=VARCHAR},#{applicationConsumables.orgName,jdbcType=VARCHAR},#{applicationConsumables.ws980090935,jdbcType=VARCHAR},#{applicationConsumables.ws980090936,jdbcType=VARCHAR},#{applicationConsumables.zj99JhSbs,jdbcType=NUMERIC},#{applicationConsumables.zj99TyCgCs,jdbcType=NUMERIC},#{applicationConsumables.zj99TyDbCs,jdbcType=NUMERIC},#{applicationConsumables.zj99CgJhZxs,jdbcType=NUMERIC},#{applicationConsumables.zj99CgZs,jdbcType=NUMERIC},#{applicationConsumables.zj99GysPsWczs,jdbcType=NUMERIC},#{applicationConsumables.zj99XpsZs,jdbcType=NUMERIC})</foreach>;END ;</insert>

代码

    @Overridepublic boolean insertBatch(Map param) throws Exception{List list = JsonArrayToPojoList.jsonToList(param, ApplicationConsumablesPojo.class);int result = 1;SqlSession batchSqlSession = null;try {ApplicationConsumablesPojo pojo= (ApplicationConsumablesPojo)list.get(0);applicationConsumablesMapper.deleteBatch(pojo.getGenerateDate(),pojo.getOrgId());batchSqlSession = this.sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);// 获取批量方式的sqlsessionint batchCount = 500;// 每批commit的个数int batchLastIndex = batchCount;// 每批最后一个的下标long startDate=System.currentTimeMillis();for (int index = 0; index < list.size(); ) {if (batchLastIndex >= list.size()) {batchLastIndex = list.size();result = result + applicationConsumablesMapper.insertBatch(list.subList(index, batchLastIndex));batchSqlSession.commit();//清理缓存,防止溢出batchSqlSession.clearCache();System.out.println("index:" + index + " batchLastIndex:" + batchLastIndex);break;// 数据插入完毕,退出循环} else {result = result + applicationConsumablesMapper.insertBatch(list.subList(index, batchLastIndex));batchSqlSession.commit();//清理缓存,防止溢出batchSqlSession.clearCache();System.out.println("index:" + index + " batchLastIndex:" + batchLastIndex);index = batchLastIndex;// 设置下一批下标batchLastIndex = index + (batchCount - 1);}}long end=System.currentTimeMillis();long costTime=end-startDate;log.info("----------------------------"+costTime+"--------------------");batchSqlSession.commit();} catch (Exception e) {e.printStackTrace();return false;} finally {batchSqlSession.close();}return true;}

利用标签

mybatis的xml编写

    <insert id="insertBatch2" useGeneratedKeys="false" parameterType="list">INSERT INTO APPLICATION_CONSUMABLES_TB(BUSINESS_ID,GENERATE_DATE,CREATE_DATE,UPDATE_DATE,ARCHIVE_DATE,PATIENT_ID,AREA_ID,ORG_ID,ORG_NAME,WS98_00_909_35,WS98_00_909_36,ZJ99_JH_SBS,ZJ99_TY_CG_CS,ZJ99_TY_DB_CS,ZJ99_CG_JH_ZXS,ZJ99_CG_ZS,ZJ99_GYS_PS_WCZS,ZJ99_XPS_ZS)(<foreach collection ="list" item="applicationConsumables" index="index" separator="union all">select#{applicationConsumables.businessId,jdbcType=VARCHAR},decode(#{applicationConsumables.generateDate,jdbcType=DATE},null,null,to_date(to_char(#{applicationConsumables.generateDate,jdbcType=DATE},'yyyy-MM-dd'),'yyyy-MM-dd')),decode(#{applicationConsumables.createDate,jdbcType=DATE},null,sysdate,to_date(#{applicationConsumables.createDate,jdbcType=DATE},'yyyy-MM-dd')),decode(#{applicationConsumables.updateDate,jdbcType=DATE},null,sysdate,to_date(to_char(#{applicationConsumables.updateDate,jdbcType=DATE},'yyyy-MM-dd'),'yyyy-MM-dd')),decode(#{applicationConsumables.archiveDate,jdbcType=DATE},null,null,to_date(to_char(#{applicationConsumables.archiveDate,jdbcType=DATE},'yyyy-MM-dd'),'yyyy-MM-dd')),#{applicationConsumables.patientId,jdbcType=VARCHAR},#{applicationConsumables.areaId,jdbcType=VARCHAR},#{applicationConsumables.orgId,jdbcType=VARCHAR},#{applicationConsumables.orgName,jdbcType=VARCHAR},#{applicationConsumables.ws980090935,jdbcType=VARCHAR},#{applicationConsumables.ws980090936,jdbcType=VARCHAR},#{applicationConsumables.zj99JhSbs,jdbcType=NUMERIC},#{applicationConsumables.zj99TyCgCs,jdbcType=NUMERIC},#{applicationConsumables.zj99TyDbCs,jdbcType=NUMERIC},#{applicationConsumables.zj99CgJhZxs,jdbcType=NUMERIC},#{applicationConsumables.zj99CgZs,jdbcType=NUMERIC},#{applicationConsumables.zj99GysPsWczs,jdbcType=NUMERIC},#{applicationConsumables.zj99XpsZs,jdbcType=NUMERIC}from dual</foreach>)</insert>

代码

    @Overridepublic boolean insertBatch2(Map param) throws Exception {List list = JsonArrayToPojoList.jsonToList(param, ApplicationConsumablesPojo.class);int nums = 500; // 一次插入200条int times = (int)Math.ceil((float)list.size() / nums);// 插入次数try{ApplicationConsumablesPojo pojo= (ApplicationConsumablesPojo)list.get(0);applicationConsumablesMapper.deleteBatch(pojo.getGenerateDate(),pojo.getOrgId());for(int i=0; i < times; i++){if(i == times - 1) {applicationConsumablesMapper.insertBatch2(list.subList(i * nums, list.size()));}else {applicationConsumablesMapper.insertBatch2(list.subList(i * nums, (i+1) * nums));}}} catch(Exception e){return false;}return true;}

三种方式耗时对比

批量插入条数200条时
| 方式 | 数据量|耗时(秒) |
|–|–|–|–|
| 利用存储过程实现批量插入 | 2万 |22.233|
| 利用foreach标签 | 2万 |13.023|
| 使用oracle的insert all | 2万 |17.489|

批量插入条数500条时
| 方式 | 数据量|耗时(秒) |
|–|–|–|–|
| 利用存储过程实现批量插入 | 2万 |16.906|
| 利用foreach标签 | 2万 |13.058|
| 使用oracle的insert all | 2万 |16.139|
批量插入条数1000条时
| 方式 | 数据量|耗时(秒) |
|–|–|–|–|
| 利用存储过程实现批量插入 | 2万 |60.583|
| 利用foreach标签 | 2万 |13.124|
| 使用oracle的insert all | 2万 |16.478|
每次写入的数量,以及网络环境对花费的时间也是有很大的影响的。

Mybatis+Oracle插入万条数据相关推荐

  1. 【SpringBoot项目中使用Mybatis批量插入百万条数据】

    SpringBoot项目中使用Mybatis批量插入百万条数据 话不多说,直接上代码,测试原生批处理的效率 开始测试 背景:因为一些业务问题,需要做多数据源,多库批量查询.插入操作,所以就研究了一下. ...

  2. 利用python脚本一键为MySQL插入万条数据

    运行环境:Windows 10 技术栈:python3,MySQL8.x 编写目的 MySQL是我们研究开发时最常用的关系型数据库.当需要向MySQL数据库中插入大量数据时,一次一条地插入显然不得劲. ...

  3. java使用mybatis一次性插入多条数据

    项目场景: java使用ibatis作为持久层框架时如何一次性插入多条数据 问题描述 正常插入一条数据的代码是这样的 mapper.insert(model) 问题分析: 这样单条插入是没有问题,问题 ...

  4. 公司新来个同事,MyBatis批量插入10w条数据仅用2秒,拍案叫绝!

    批量插入功能是我们日常工作中比较常见的业务功能之一,今天咱们来一个 MyBatis 批量插入的汇总篇,同时对 3 种实现方法做一个性能测试,以及相应的原理分析. 先来简单说一下 3 种批量插入功能分别 ...

  5. mybatis insert 插入多条数据

    普通sql中insert插入多条 insert into 表名(字段名1,字段名2)values(值a1,值b1), (值a2,值b2), 例如: insert into user_info (use ...

  6. java oracle in 10000_oracle循环插入1万条数据

    declare maxnumber constant number:=10000; i number :=1; begin for i in 1..maxnumber loop insert into ...

  7. 对于一万条数据量使用Oracle游标,存储过程,一般查询的速度的对比

    一,创建ID自增长表格 1,创建序列 create sequence my_em_seq --序列名minvalue 1--最小值maxvalue 99999999999999999999999999 ...

  8. Java怎么实现几十万条数据插入(30万条数据插入MySQL仅需13秒)

    本文主要讲述通过MyBatis.JDBC等做大数据量数据插入的案例和结果. 30万条数据插入插入数据库验证 实体类.mapper和配置文件定义 User实体 mapper接口 mapper.xml文件 ...

  9. mysql20数据_mysql 插入20万条数据

    1. 用c语言写20万条数据 #include void pro_ID(char * str,inti){if(i%17576==0){++*(str+1);*(str+2)=97;*(str+3)= ...

最新文章

  1. 联邦学习,为何而生?
  2. csu1356 :判断一个环是否为奇数环
  3. [BUUCTF-pwn]——mrctf2020_shellcode
  4. NYOJ516(优化)
  5. Windows Server 2012系列之一安装初体验
  6. SwiftUI3.0用户登录输入非空校验经典案例
  7. java和vue2.0
  8. 将SqlServer的数据导出到Excel/csv中的各种方法 .
  9. UML从需求到实现----用例
  10. Hibernate--什么是持久化?
  11. 实验三+124+高小娟
  12. ifs 报表开发手册_.NET快速开发框架Colder发布:10 篇热文汇总
  13. O2O新猜想:如果商家这样做,还需要团购平台吗
  14. 国产手机已经用上了 120W 快充技术,苹果还在用20W的原因
  15. 计算机毕业设计java+ssm酒店管理系统(源码+系统+mysql数据库+Lw文档)
  16. win10 64位注册TeeChart8.ocx
  17. Oracle undo表空间管理
  18. Deen Smart隐私政策
  19. ArcMap学习笔记(七)地图制作
  20. 麒麟操作系统软件更新灾难连篇之二:QQ罢工

热门文章

  1. 是否有刘海的机型(iPhoneX iPhoneXR iPhoneXS iPhoneXSMax)适配判断
  2. 纯CSS实现横向滚动条
  3. 关于ATIS以及基于注意力机制的递归神经网络模型 的学习记录
  4. 速看丨传智教育全链路+UI/UE设计学科V6.0硬核来袭!
  5. [论文阅读](SHAPING DATASETS: OPTIMAL DATA SELECTION FOR SPECIFIC TARGET DISTRIBUTIONS ACROSS DIMENSIONS)
  6. 02: DNS服务基础 特殊解析 DNS子域授权 缓存DNS 总结和答疑
  7. python dtype(0)_Python numpy,创建数组,数据类型,dtype属性
  8. 如何提高自己的深度思考能力
  9. 12、pandas 数据类型转换
  10. 按键精灵手机版 山海插件 Call shanhai.SetIME(1) 设置讯飞输入法无效