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è)計(jì)時需要將其設(shè)計(jì)成帶進(jìn)度展示的異步任務(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>
映射實(shí)例接口和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)驗(yàn)證了一下,插入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是有問題的。
實(shí)際產(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)文章
SpringBoot啟動報(bào)錯Whitelabel Error Page: This&nbs
當(dāng)我們使用Spring Boot框架開發(fā)Web應(yīng)用時,有時會遇到啟動報(bào)錯信息為"Whitelabel Error Page: This application has no explicit mapping for",種報(bào)錯信息意味著我們的應(yīng)用缺少某個URL映射的配置,導(dǎo)致請求無法處理,在本篇文章中,我們將詳細(xì)討論如何解決這個問題2024-03-03關(guān)于yml文件字符串,List,Map的書寫方式并使用@ConfigurationProperties注入配置類
這篇文章主要介紹了關(guān)于yml文件字符串,List,Map的書寫方式并使用@ConfigurationProperties注入配置類,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12Java實(shí)現(xiàn)文件上傳至服務(wù)器的方法
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)文件上傳至服務(wù)器的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01mybatis?mapper.xml中如何根據(jù)數(shù)據(jù)庫類型選擇對應(yīng)SQL語句
這篇文章主要介紹了mybatis?mapper.xml中如何根據(jù)數(shù)據(jù)庫類型選擇對應(yīng)SQL語句,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01SpringBoot之使用Feign實(shí)現(xiàn)微服務(wù)間的交互
這篇文章主要介紹了SpringBoot中使用Feign實(shí)現(xiàn)微服務(wù)間的交互,對微服務(wù)這方面感興趣的小伙伴可以參考閱讀本文2023-03-03Spring?Boot集成RabbitMQ以及隊(duì)列模式操作
RabbitMQ是實(shí)現(xiàn)AMQP(高級消息隊(duì)列協(xié)議)的消息中間件的一種,下面這篇文章主要給大家介紹了關(guān)于Spring?Boot集成RabbitMQ以及隊(duì)列模式操作的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04