当前位置:网站首页>How to batch insert 100000 pieces of data
How to batch insert 100000 pieces of data
2022-06-11 14:57:00 【Ant】
How to batch insert 10 Ten thousand data
Ideas
When inserting in batches , There are generally two ways of thinking :
- Use one for loop , Insert data one by one ( This needs to start batch processing ).
- Generate an insert sql, Like this
insert into user(username,address) values('aa','bb'),('cc','dd')....
This problem is mainly considered from two aspects :
- SQL Own execution efficiency
- The Internet I/O
First option
Use for loop :
- The advantage of this scheme is that ,JDBC Medium PreparedStatement It has precompile function , After precompiling, it will be cached , hinder SQL Execution will be faster and JDBC Batch processing can be started , This batch process is awesome. .
- The disadvantage is , A lot of times our SQL The server and application server may not be the same , Therefore, the network must be considered IO, If the Internet IO If it takes more time , Then it may slow down SQL Speed of execution .
When using for When inserting one by one , The batch mode needs to be enabled (BATCH), In this way, only one SqlSession, If batch mode is not used , Repeated acquisition Connection And release Connection It's going to take a lot of time , Efficiency is very low .
Second option
Generate a sql Insert :
- The advantage of this scheme is that there is only one network IO, Even slicing is only a few times IO, So this scheme will not be used in the network IO Spend too much time on .
- Of course, this scheme has several disadvantages , One is SQL Is too long. , It may even require batch processing after slicing ; Second, it can not give full play PreparedStatement The advantages of precompiling ,SQL To be re parsed and cannot be reused ; The third is the final generation SQL Is too long. , The database manager parses such a long SQL It also takes time .
The final consideration is in the network IO Time spent on , Whether it exceeds SQL The time of insertion ? This is the core issue we should consider .
Select the corresponding batch insertion method according to the actual situation .
Mybatis Plus How to do it
Actually MyBatis Plus There is also a batch insertion method saveBatch, Let's take a look at its implementation source code :
@Transactional(rollbackFor = Exception.class)
@Override
public boolean saveBatch(Collection<T> entityList, int batchSize) {
String sqlStatement = getSqlStatement(SqlMethod.INSERT_ONE);
return executeBatch(entityList, batchSize, (sqlSession, entity) -> sqlSession.insert(sqlStatement, entity));
} I got it here sqlStatement It's just one. INSERT_ONE, That is, insert one by one .
executeBatch Method :
public static <E> boolean executeBatch(Class<?> entityClass, Log log, Collection<E> list, int batchSize, BiConsumer<SqlSession, E> consumer) {
Assert.isFalse(batchSize < 1, "batchSize must not be less than one");
return !CollectionUtils.isEmpty(list) && executeBatch(entityClass, log, sqlSession -> {
int size = list.size();
int i = 1;
for (E element : list) {
consumer.accept(sqlSession, element);
if ((i % batchSize == 0) || i == size) {
sqlSession.flushStatements();
}
i++;
}
});
} Note here return The third parameter in , It's a lambda expression , This is also MP Core logic of batch insertion in , You can see ,MP Divide the data first ( The default tile size is 1000), After slicing , It is also inserted one by one . Keep looking at executeBatch Method , You'll find out sqlSession In fact, it is also a batch process sqlSession, It's not ordinary sqlSession.
Reference material
Sharing plans
Blog content will be synchronized to Tencent cloud + Community , Invite everyone to join us :https://cloud.tencent.com/
license agreement
In this paper A signature - Noncommercial use - Share in the same way 4.0 The international license agreement , Reprint please indicate the source .
边栏推荐
- What is excess product power? Find the secret key of the second generation cs75plus in the year of the tiger
- LeetCode每日一题——加一
- Raspberry pie obtains the function of network installation system without the help of other devices
- Task manager based on Qt development
- Social software soul withdraws its IPO application: Tencent is a major shareholder
- Global and China dynamic light scattering nano laser particle sizer market depth research and Analysis Report
- Implementation of gray-scale publishing scheme for microservice architecture based on gateway and Nacos
- In depth interpretation: distributed system resilience architecture ballast openchaos
- 大道至简 | 设计 ViT 到底怎么配置Self-Attention才是最合理的?
- Hashicopy之nomad应用编排方案03(运行一个job)
猜你喜欢

3年亏损136亿,上市能救活威马吗?

Implementation of gray-scale publishing scheme for microservice architecture based on gateway and Nacos

Simple C language address book

Cartoon: interesting "cake cutting" problem

树莓派知识大扫盲

基于 GateWay 和 Nacos 实现微服务架构灰度发布方案

Backtracking / solution space tree permutation tree

19. 二叉搜索树的插入删除修剪

【SystemVerilog 之 接口】~ Interface

一些经典的嵌入式C面试题汇总
随机推荐
树莓派知识大扫盲
B站高管解读财报:疫情对公司长期发展无影响 视频化趋势不可阻挡
Database optimization
Uniapp settings page Jump effect - navigateto switching effect - Global animationtype animation
2021 go developer survey
NVIDIA R & D director: how does AI improve chip design?
Hashicopy之nomad应用编排方案04(伸缩及更新一个Job)
Sum of two leetcode numbers
3年亏损136亿,上市能救活威马吗?
Guess numbers games
Hashicopy之nomad应用编排方案02
【SystemVerilog 之 验证】~ 测试平台、硬件设计描述、激励发生器、监测器、比较器
Repository Manager之Nexus
一些经典的嵌入式C面试题汇总
Tencent interviewers share their interview experience, how to evaluate the interviewers' technical and personal comprehensive quality, and give you some suggestions on the interview
Elk log analysis system
Flutter 3.0 was officially released: it stably supports 6 platforms, and byte jitter is the main user
腾讯面试官分享面试经验,如何考察面试者技术及个人综合素质,给正在面试的你一点建议
uniapp设置页面跳转效果 - navigateTo切换效果 - 全局animationType动画
Tangzhengrong: CTO is the intersection of business thinking and technical thinking