怎樣提高mybatis-plus中saveBatch方法的效率
提高mybatis-plus中saveBatch方法的效率
MyBatis-Plus中的saveBatch方法是一個(gè)很方便的批量插入數(shù)據(jù)的方法,但是如果插入的數(shù)據(jù)量很大時(shí),可能會(huì)出現(xiàn)效率較低的情況。
提高saveBatch方法效率的方法
1.批量插入的數(shù)據(jù)量不宜過(guò)大,否則可能會(huì)導(dǎo)致內(nèi)存溢出。建議根據(jù)實(shí)際情況選擇合適的批量插入數(shù)據(jù)的數(shù)量。
2.如果插入的數(shù)據(jù)是從文件或其他數(shù)據(jù)源中讀取的,可以使用流式插入的方式,將數(shù)據(jù)流分批插入數(shù)據(jù)庫(kù),可以減小內(nèi)存壓力。
3.在執(zhí)行saveBatch方法前,可以通過(guò)開(kāi)啟MyBatis-Plus的批量插入功能,將多條SQL語(yǔ)句合并成一條執(zhí)行,減少與數(shù)據(jù)庫(kù)的交互次數(shù)??梢酝ㄟ^(guò)以下代碼開(kāi)啟批
mybatis-plus saveOrUpdateBatch踩坑
mybatis-plus版本: 3.5.1
調(diào)用方法
@Transactional(rollbackFor = Exception.class) ?
default boolean saveOrUpdateBatch(Collection<T> entityList) { ?
? ? return saveOrUpdateBatch(entityList, DEFAULT_BATCH_SIZE); ?
}問(wèn)題說(shuō)明
當(dāng)對(duì)entityList進(jìn)行批量更新操作時(shí),方法內(nèi)部會(huì)根據(jù)主鍵查詢?cè)撚涗?,?dǎo)致批量更新操作十分緩慢,具體代碼如下
@Transactional(rollbackFor = Exception.class)
@Override
public boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize) {
? ? TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
? ? Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!");
? ? String keyProperty = tableInfo.getKeyProperty();
? ? Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!");
? ? return SqlHelper.saveOrUpdateBatch(this.entityClass, this.mapperClass, this.log, entityList, batchSize, (sqlSession, entity) -> {
? ? ? ? Object idVal = tableInfo.getPropertyValue(entity, keyProperty);
? ? ? ? // 當(dāng)主鍵存在時(shí),會(huì)執(zhí)行sqlSession.selectList(getSqlStatement(SqlMethod.SELECT_BY_ID), entity)查詢?cè)摋l記錄
? ? ? ? return StringUtils.checkValNull(idVal) || CollectionUtils.isEmpty(sqlSession.selectList(getSqlStatement(SqlMethod.SELECT_BY_ID), entity));
? ? }, (sqlSession, entity) -> {
? ? ? ? MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>();
? ? ? ? param.put(Constants.ENTITY, entity);
? ? ? ? sqlSession.update(getSqlStatement(SqlMethod.UPDATE_BY_ID), param);
? ? });
}public static <E> boolean saveOrUpdateBatch(Class<?> entityClass, Class<?> mapper, Log log, Collection<E> list, int batchSize, BiPredicate<SqlSession, E> predicate, BiConsumer<SqlSession, E> consumer) {
?? ?String sqlStatement = getSqlStatement(mapper, SqlMethod.INSERT_ONE);
?? ?return executeBatch(entityClass, log, list, batchSize, (sqlSession, entity) -> {
?? ??? ?// 執(zhí)行predicate
?? ??? ?if (predicate.test(sqlSession, entity)) {
?? ??? ??? ?sqlSession.insert(sqlStatement, entity);
?? ??? ?} else {
?? ??? ??? ?consumer.accept(sqlSession, entity);
?? ??? ?}
?? ?});
}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 idxLimit = Math.min(batchSize, size);
?? ??? ?int i = 1;
?? ??? ?for (E element : list) {
?? ??? ??? ?// 循環(huán)每條記錄,執(zhí)行consumer,在此次調(diào)用中,consumer中會(huì)執(zhí)行predicate,當(dāng)該條記錄主鍵不為空時(shí),會(huì)通過(guò)主鍵查詢?cè)撚涗?
?? ??? ??? ?consumer.accept(sqlSession, element);
?? ??? ??? ?if (i == idxLimit) {
?? ??? ??? ??? ?sqlSession.flushStatements();
?? ??? ??? ??? ?idxLimit = Math.min(idxLimit + batchSize, size);
?? ??? ??? ?}
?? ??? ??? ?i++;
?? ??? ?}
?? ?});
}總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Kotlin 基礎(chǔ)教程之?dāng)?shù)組容器
這篇文章主要介紹了Kotlin 基礎(chǔ)教程之?dāng)?shù)組容器的相關(guān)資料,需要的朋友可以參考下2017-06-06
Java socket通信模擬QQ實(shí)現(xiàn)多人聊天室
Socket在Java實(shí)戰(zhàn)網(wǎng)絡(luò)通信編程應(yīng)用中有非常重要的作用,你想要跟別人聯(lián)系都得通過(guò)socket占據(jù)端口來(lái)實(shí)現(xiàn),掌握Socket技術(shù)不僅在聊天應(yīng)用程序中需要用到(比如QQ什么的都都是用socket來(lái)寫(xiě)的),而且對(duì)于學(xué)習(xí) Asp.net 也非常有幫助2022-07-07
VS?Code中運(yùn)行Java?SpringBoot的項(xiàng)目詳細(xì)步驟
這篇文章主要介紹了VS?Code中運(yùn)行Java?SpringBoot項(xiàng)目的相關(guān)資料,文中涵蓋了安裝必要的擴(kuò)展、配置環(huán)境、創(chuàng)建或?qū)腠?xiàng)目、配置調(diào)試環(huán)境、運(yùn)行和調(diào)試項(xiàng)目、使用Spring?Boot?Actuator以及配置任務(wù)自動(dòng)化等步驟,需要的朋友可以參考下2024-12-12
JAVA?SSE接口開(kāi)發(fā)中的Spring?Security與異步線程池配置方法
本文詳細(xì)描述了在使用SpringSecurity和異步線程池進(jìn)行SSE接口開(kāi)發(fā)時(shí)遇到的兩個(gè)問(wèn)題:AccessDeniedException異常和SimpleAsyncTaskExecutor警告,文章還介紹了線程池參數(shù)的科學(xué)估算方法,并適用于SpringBoot/SpringMVC的SSE接口生產(chǎn)環(huán)境配置,感興趣的朋友一起看看吧2025-12-12
springboot 項(xiàng)目某個(gè)接口響應(yīng)特別慢問(wèn)題排查分析
文章描述了一個(gè)Spring Boot項(xiàng)目中某個(gè)接口請(qǐng)求日志顯示耗時(shí)極短,但通過(guò)Postman發(fā)起相同請(qǐng)求時(shí)耗時(shí)顯著增加的問(wèn)題,經(jīng)過(guò)排查,發(fā)現(xiàn)耗時(shí)主要集中在自定義的ControllerInterceptor中的logBefore方法,特別是SensitiveInfoSerialize.getJson方法,感興趣的朋友跟隨小編一起看看吧2025-12-12

