Mybatis批量插入數(shù)據(jù)的兩種方式總結(jié)與對比
總體描述
軟件開發(fā)過程中需要批量插入數(shù)據(jù)的場景有幾種:
- 從離線文件(excel, csv等)導(dǎo)入大批量數(shù)據(jù)到系統(tǒng)。
- 從其它系統(tǒng)定時或者人工同步大批量數(shù)據(jù)到系統(tǒng)。
- 程序自身的某些算法執(zhí)行時會生成大批量數(shù)據(jù)保存到數(shù)據(jù)庫。
上面這些場景都是長時間的處理過程,在軟件設(shè)計時需要將其設(shè)計成帶進度展示的異步任務(wù)(同步任務(wù)微服務(wù)有http請求超時的風(fēng)險)。異步任務(wù)可以使用消息框架。
使用批量插入技術(shù)能提升數(shù)據(jù)持久化的性能。用mybatis有兩種批量插入數(shù)據(jù)的方式可選:1. 拼接批量插入多條數(shù)據(jù)的SQL. 2. 使用Batch Insert技術(shù)。
方式一:拼接插入多條數(shù)據(jù)的SQL
mapper接口代碼
/**
* 插入數(shù)據(jù)列表
*
* @param dataList 數(shù)據(jù)列表
*/
void insertDataList(@Param("list") List<BatchData> dataList);
XML文件配置
<insert id="batchInsertData" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
INSERT INTO t_batch_data (
column1,
column2,
column3,
column4,
column5,
column6,
column7,
column8,
column9,
column10
) VALUES
<foreach item="data" collection="list" separator=",">
(
#{data.column1},
#{data.column2},
#{data.column3},
#{data.column4},
#{data.column5},
#{data.column6},
#{data.column7},
#{data.column8},
#{data.column9},
#{data.column10}
)
</foreach>
</insert>
可以看到,XML配置文件使用 foreach 對多條數(shù)據(jù)做了拼接,Value部分用逗號分隔。拼接后的SQL樣式:
INSERT INTO t_batch_data (
column1,
column2,
column3,
column4,
column5,
column6,
column7,
column8,
column9,
column10
) VALUES
(
?,
?,
?,
?,
?,
?,
?,
?,
?,
?
)
,
(
?,
?,
?,
?,
?,
?,
?,
?,
?,
?
)
,
(
?,
?,
?,
?,
?,
?,
?,
?,
?,
?
)
可以看到,拼接的SQL長度跟批量插入數(shù)據(jù)的條數(shù)和單條數(shù)據(jù)的字段數(shù)相關(guān)。對于像postgres這樣限定了參數(shù)個數(shù)的數(shù)據(jù)庫,需要提前對大批量數(shù)據(jù)做拆分處理。
下面的示例代碼對批量數(shù)據(jù)按200條一組做拆分,然后再入庫。
public long foreachBatchInsert(@PathVariable("amount") int amount) {
long beginTime = System.currentTimeMillis();
List<BatchData> dataList = buildDataList(amount);
// 大數(shù)據(jù)分批處理入庫
List<List<BatchData>> dataGroup = ListUtil.splitList(dataList, 200);
for (List<BatchData> group : dataGroup) {
batchInsertMapper.insertDataList(group);
}
return System.currentTimeMillis() - beginTime;
}
方式二: 使用Batch Insert技術(shù)
Mapper接口代碼
/**
* 插入單條數(shù)據(jù)
*
* @param data PO數(shù)據(jù)
*/
void insertData(@Param("data") BatchData data);XML文件配置
<insert id="insertData" useGeneratedKeys="true" keyProperty="data.id" keyColumn="id">
INSERT INTO t_batch_data (
column1,
column2,
column3,
column4,
column5,
column6,
column7,
column8,
column9,
column10
) VALUES (
#{data.column1},
#{data.column2},
#{data.column3},
#{data.column4},
#{data.column5},
#{data.column6},
#{data.column7},
#{data.column8},
#{data.column9},
#{data.column10}
)
</insert>
映射實例接口和SQL代碼與插入單個對象無異。關(guān)鍵代碼在應(yīng)用層。
應(yīng)用層代碼
public long mybatisBatchInsert(@PathVariable("amount") int amount) {
SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
long beginTime = System.currentTimeMillis();
try {
BatchInsertMapper insertMapper = session.getMapper(BatchInsertMapper.class);
List<BatchData> dataList = buildDataList(amount);
for (BatchData data : dataList) {
insertMapper.insertData(data);
}
session.commit();
session.clearCache();
} catch (Exception e) {
session.rollback();
} finally {
session.close();
}
return System.currentTimeMillis() - beginTime;
}
查看打印出執(zhí)行的SQL語句:
INSERT INTO t_batch_data (
column1,
column2,
column3,
column4,
column5,
column6,
column7,
column8,
column9,
column10
) VALUES (
?,
?,
?,
?,
?,
?,
?,
?,
?,
?
)
攔截StatementHandler的prepare執(zhí)行方法,可以看到只執(zhí)行了一次預(yù)編譯。批量插入不會出現(xiàn)參數(shù)個數(shù)超限或者SQL語句超長的問題。
對比分析
性能對比
在postgres數(shù)據(jù)庫中新建了一個包含10個text類型字段的表(t_batch_data)驗證了一下,插入20萬條數(shù)據(jù)時間都在15秒左右,相差不大。方案1必須做分組(參數(shù)個數(shù)超過限制);方案二本身是調(diào)用的mapper的插入單個對象的接口, 不需要做分批。
應(yīng)用場景分析
如表字段是固定的,字段數(shù)量也不大可以使用方案一;如表字段數(shù)量不固定(元數(shù)據(jù)驅(qū)動)推薦使用第二種方案。第二種方案在代碼執(zhí)行到session.commit()時數(shù)據(jù)才真正入庫,如果在這之前使用數(shù)據(jù)庫的數(shù)據(jù)或者回填的自增ID是有問題的。
實際產(chǎn)品開發(fā)過程中,即使采用第二種方案也建議對大數(shù)量做分組處理,將單次操作數(shù)據(jù)庫的時間控制在2秒以內(nèi)。
Demo代碼地址: https://github.com/ylforever/elon-postgres.git
總結(jié)
到此這篇關(guān)于Mybatis批量插入數(shù)據(jù)的兩種方式總結(jié)與對比的文章就介紹到這了,更多相關(guān)Mybatis批量插入數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java異常--常見方法--自定義異常--增強try(try-with-resources)詳解
這篇文章主要介紹了Java異常--常見方法--自定義異常--增強try(try-with-resources)的相關(guān)知識,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03
Spring中的底層架構(gòu)核心概念類型轉(zhuǎn)換器詳解
這篇文章主要介紹了Spring中的底層架構(gòu)核心概念類型轉(zhuǎn)換器詳解,本文結(jié)合示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12
IntelliJ IDEA 2019.1.1 for MAC 下載和注
這篇文章主要介紹了IntelliJ IDEA 2019.1.1 for MAC 下載和注冊碼激活,教程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04
Java 通過AQS實現(xiàn)數(shù)據(jù)組織
這篇文章主要介紹了通過AQS實現(xiàn)數(shù)據(jù)組織,想了解AQS的同學(xué)可以參考下2021-04-04
Java之Rsync并發(fā)遷移數(shù)據(jù)并校驗詳解
這篇文章主要介紹了Java之Rsync并發(fā)遷移數(shù)據(jù)并校驗詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08
java對接webservice接口的4種方式總結(jié)
因工作需要和一個Sap相關(guān)系統(tǒng)以WebService的方式進行接口聯(lián)調(diào),之前僅聽過這種技術(shù),但并沒有實操過,所以將本次開發(fā)進行記錄,這篇文章主要給大家介紹了關(guān)于java對接webservice接口的4種方式,需要的朋友可以參考下2023-10-10

