mybatis批量插入oracle大量数据记录性能问题解决
环境: mybatis + oracle11g r2
1.使用"直接路径插入"(以下sql语句中的"/*+append_values */"),而且使用keyword"union all":
<insert id="addUidCodeBatch" parameterType="java.util.List">
insert into /*+append_values */
T_UID_CODE(C_UID_CODE,
C_SERAIL_LEN,
C_BATCH_CODE,
C_TYPE,
C_CREATE_TIME,
C_SUPER_CODE,
c_security_code,
C_SERIAL_CODE
)
<foreach collection="list" item="item" index="index" separator="union all" >
select #{item.uidCode},
#{item.kCode},
#{item.batchCode},
#{item.type},
sysdate,
#{item.superCode},
#{item.securityCode},
#{item.serialCode}
from dual
</foreach>
</insert>
2.dao层实现: 之前是一次性commit,这样做会随着插入数目的增大,运行速度陡然变慢,所以应该分批次进行插入:
public void save(List<UidCodeBean> uidCodeList) throws Exception {
SqlSession batchSqlSession = null;
try {
batchSqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);//获取批量方式的sqlsession
int batchCount = 1000;//每批commit的个数
int batchLastIndex = batchCount - 1;//每批最后一个的下标
for(int index = 0; index < uidCodeList.size()-1;){
if(batchLastIndex > uidCodeList.size()-1){
batchLastIndex = uidCodeList.size() - 1;
batchSqlSession.insert(NAMESPACE+".addUidCodeBatch", uidCodeList.subList(index, batchLastIndex));
batchSqlSession.commit();
System.out.println("index:"+index+" batchLastIndex:"+batchLastIndex);
break;//数据插入完成,退出循环
}else{
batchSqlSession.insert(NAMESPACE+".addUidCodeBatch", uidCodeList.subList(index, batchLastIndex)); batchSqlSession.commit();
System.out.println("index:"+index+" batchLastIndex:"+batchLastIndex);
index = batchLastIndex + 1;//设置下一批下标
batchLastIndex = index + (batchCount - 1);
}
}
}finally{
batchSqlSession.close();
}
}
mybatis批量插入oracle大量数据记录性能问题解决的更多相关文章
- mybatis批量插入oracle时报错:unique constraint (table name) violated
mybatis批量插入oracle时报错:unique constraint (table name) violated,是因为插入的集合中有两条相同唯一约束的数据.
- 一次EF批量插入多表数据的性能优化经历
距离上次的博客已经有15个多月了,感慨有些事情还是需要坚持,一旦停下来很有可能就会停很久或者从此再也不会坚持.但我个人一直还坚持认为属于技术狂热份子,且喜欢精益求精的那种.最近遇到两个和数据迁移相关的 ...
- mybatis批量插入oracle
<insert id="batchInsert" parameterType="java.util.List"> INSERT INTO TEST( ...
- Mybatis批量插入oracle,mysql
oracle <insert id="addUserData" parameterType="java.util.List"> INSERT IN ...
- mybatis批量插入数据到oracle
mybatis 批量插入数据到oracle报 ”java.sql.SQLException: ORA-00933: SQL 命令未正确结束“ 错误解决方法 oracle批量插入使用 insert a ...
- oracle+mybatis批量插入踩坑记
最近在项目中需要使用oracle+mybatis批量插入数据,因为自增主键,遇到问题,现记录如下: 一.常用的两种sql写法报错 1.insert ... values ... <insert ...
- Mybatis 批量插入数据
--mybatis 批量插入数据 --1.Oracle(需要测试下是否支持MySQL) < insert id ="insertBatch" parameterType=&q ...
- MyBatis批量插入性能及问题
1.mybatis三种批量插入方式对比 2.Mybatis与JDBC批量插入MySQL数据库性能测试及解决方案 3.Mybatis批量插入引发的血案 4.Oracle批量插入数据SQL语句太长出错
- MyBatis 批量插入数据的 3 种方法!
批量插入功能是我们日常工作中比较常见的业务功能之一,之前我也写过一篇关于<MyBatis Plus 批量数据插入功能,yyds!>的文章,但评论区的反馈不是很好,主要有两个问题:第一,对 ...
随机推荐
- Java多线程演示样例(模拟通话,sleep,join,yield,wait,notify,Semaphore)
主线程等待子线程的多种方法 synchronized浅析 sleep 是静态方法,Thread.sleep(xx)谁调用谁睡眠. join 是合并方法.当前线程调用其它线程xx.join()则等到xx ...
- python 并发编程入门
多进程 在Unix/Linux下,为我们提供了类似c中<unistd.h>头文件里的的fork()函数的接口,这个函数位于os模块中,相同与c中类似,对于父进程fork()调用返回子进程I ...
- LeetCode——Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- 41.使用SAX读取XML
main.cpp #include <QtGui> #include <iostream> #include "saxhandler.h" int main ...
- 限制textfield的文字长度
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementSt ...
- init&initWithFrame&initWithCoder
//当我们所写的程序里没用用Nib文件(XIB)时,用代码控制视图内容,需要调用initWithFrame去初始化 - (id)initWithFrame:(CGRect)frame { if (se ...
- github结合TortoiseGit使用sshkey,无需每次输入账号和密码
首先需要明确,github上支持三种方式进行项目的clone https,ssh,subversion ssh的方式 git@github.com:用户名/版本库t.git ...
- SQL CASE WHEN语句性能优化
背景:性能应该是功能的一个重要参考,特别是在大数据的背景之下!写SQL语句时如果仅考虑业务逻辑,而不去考虑语句效率问题,有可能导致严重的效率问题,导致功能不可用或者资源消耗过大.其中的一种情况是,处理 ...
- Unity 自己旋转 方法
transform.Rotate(Vector3.up * Time.deltaTime * 100);
- JS判断客户端是否是iOS或者Android或者ipad(二)
js判断客户端是IPAD和iphone 多了就不说了,直接上代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 funct ...