前言
- 開發(fā)目的: 提高百萬級數(shù)據(jù)插入效率。
-
采取方案: 利用
ThreadPoolTaskExecutor
多線程批量插入。 - 采用技術(shù): springboot2.1.1+mybatisPlus3.0.6+swagger2.5.0+Lombok1.18.4+postgresql+ThreadPoolTaskExecutor等。
基于 Spring Boot + MyBatis Plus + Vue & Element 實現(xiàn)的后臺管理系統(tǒng) + 用戶小程序,支持 RBAC 動態(tài)權(quán)限、多租戶、數(shù)據(jù)權(quán)限、工作流、三方登錄、支付、短信、商城等功能
具體實現(xiàn)細(xì)節(jié)
application-dev.properties
添加線程池配置信息
> 基于 Spring Cloud Alibaba + Gateway + Nacos + RocketMQ + Vue & Element 實現(xiàn)的后臺管理系統(tǒng) + 用戶小程序,支持 RBAC 動態(tài)權(quán)限、多租戶、數(shù)據(jù)權(quán)限、工作流、三方登錄、支付、短信、商城等功能
>
> * 項目地址:
> * 視頻教程:
# 異步線程配置
# 配置核心線程數(shù)
async.executor.thread.core_pool_size = 30
# 配置最大線程數(shù)
async.executor.thread.max_pool_size = 30
# 配置隊列大小
async.executor.thread.queue_capacity = 99988
# 配置線程池中的線程的名稱前綴
async.executor.thread.name.prefix = async-importDB-
spring容器注入線程池bean對象
@Configuration
@EnableAsync
@Slf4j
publicclassExecutorConfig{
@Value("${async.executor.thread.core_pool_size}")
privateintcorePoolSize;
@Value("${async.executor.thread.max_pool_size}")
privateintmaxPoolSize;
@Value("${async.executor.thread.queue_capacity}")
privateintqueueCapacity;
@Value("${async.executor.thread.name.prefix}")
privateStringnamePrefix;
@Bean(name="asyncServiceExecutor")
publicExecutorasyncServiceExecutor(){
log.warn("startasyncServiceExecutor");
//在這里修改
ThreadPoolTaskExecutorexecutor=newVisiableThreadPoolTaskExecutor();
//配置核心線程數(shù)
executor.setCorePoolSize(corePoolSize);
//配置最大線程數(shù)
executor.setMaxPoolSize(maxPoolSize);
//配置隊列大小
executor.setQueueCapacity(queueCapacity);
//配置線程池中的線程的名稱前綴
executor.setThreadNamePrefix(namePrefix);
// rejection-policy:當(dāng)pool已經(jīng)達(dá)到max size的時候,如何處理新任務(wù)
// CALLER_RUNS:不在新線程中執(zhí)行任務(wù),而是有調(diào)用者所在的線程來執(zhí)行
executor.setRejectedExecutionHandler(newThreadPoolExecutor.CallerRunsPolicy());
//執(zhí)行初始化
executor.initialize();
returnexecutor;
}
}
創(chuàng)建異步線程 業(yè)務(wù)類
@Service
@Slf4j
publicclassAsyncServiceImplimplementsAsyncService{
@Override
@Async("asyncServiceExecutor")
publicvoidexecuteAsync(ListlogOutputResults,LogOutputResultMapperlogOutputResultMapper,CountDownLatchcountDownLatch) {
try{
log.warn("startexecuteAsync");
//異步線程要做的事情
logOutputResultMapper.addLogOutputResultBatch(logOutputResults);
log.warn("endexecuteAsync");
}finally{
countDownLatch.countDown();//很關(guān)鍵,無論上面程序是否異常必須執(zhí)行countDown,否則await無法釋放
}
}
}
創(chuàng)建多線程批量插入具體業(yè)務(wù)方法
@Override
publicinttestMultiThread(){
ListlogOutputResults=getTestData();
//測試每100條數(shù)據(jù)插入開一個線程
List>lists=ConvertHandler.splitList(logOutputResults,100);
CountDownLatchcountDownLatch=newCountDownLatch(lists.size());
for(ListlistSub:lists){
asyncService.executeAsync(listSub,logOutputResultMapper,countDownLatch);
}
try{
countDownLatch.await();//保證之前的所有的線程都執(zhí)行完成,才會走下面的;
//這樣就可以在下面拿到所有線程執(zhí)行完的集合結(jié)果
}catch(Exceptione){
log.error("阻塞異常:"+e.getMessage());
}
returnlogOutputResults.size();
}
模擬2000003 條數(shù)據(jù)進(jìn)行測試
![4fc16cce-6555-11ed-8abf-dac502259ad0.png](https://file1.elecfans.com//web2/M00/97/AF/wKgZomTnPbCAa0pPAABhWF0cc8c714.png)
多線程 測試 2000003 耗時如下:耗時1.67分鐘
![4fe0a918-6555-11ed-8abf-dac502259ad0.png](https://file1.elecfans.com//web2/M00/97/AF/wKgZomTnPbCASC_zAABjiJldS1s500.png)
![4fe8a71c-6555-11ed-8abf-dac502259ad0.png](https://file1.elecfans.com//web2/M00/97/AF/wKgZomTnPbGAJTgdAAIuNFpMYVw027.png)
本次開啟30個線程,截圖如下:
![5003382a-6555-11ed-8abf-dac502259ad0.png](https://file1.elecfans.com//web2/M00/97/AF/wKgZomTnPbGAVtHuAADKM1CcGNU580.png)
單線程測試2000003 耗時如下:耗時5.75分鐘
![502a55e0-6555-11ed-8abf-dac502259ad0.png](https://file1.elecfans.com//web2/M00/97/AF/wKgZomTnPbGAWd48AABpBHOuiqs531.png)
![504a0548-6555-11ed-8abf-dac502259ad0.png](https://file1.elecfans.com//web2/M00/97/AF/wKgZomTnPbGALsUFAAIHApjNOqQ380.png)
檢查多線程入庫的數(shù)據(jù),檢查是否存在重復(fù)入庫的問題:
根據(jù)id分組,查看是否有id重復(fù)的數(shù)據(jù),通過sql語句檢查,沒有發(fā)現(xiàn)重復(fù)入庫的問題
![506e5010-6555-11ed-8abf-dac502259ad0.png](https://file1.elecfans.com//web2/M00/97/AF/wKgZomTnPbGAFcm5AAAxhn1EJxE980.png)
檢查數(shù)據(jù)完整性:通過sql語句查詢,多線程錄入數(shù)據(jù)完整
![50798f48-6555-11ed-8abf-dac502259ad0.png](https://file1.elecfans.com//web2/M00/97/AF/wKgZomTnPbGAeuEVAAB-cQZvfcs692.png)
測試結(jié)果
不同線程數(shù)測試:
![508bc816-6555-11ed-8abf-dac502259ad0.png](https://file1.elecfans.com//web2/M00/97/AF/wKgZomTnPbGAbdwHAACUedRyB34438.png)
![50969b9c-6555-11ed-8abf-dac502259ad0.png](https://file1.elecfans.com//web2/M00/97/AF/wKgZomTnPbKAdm4gAAGBD-6F508506.png)
總結(jié)
通過以上測試案列,同樣是導(dǎo)入2000003 條數(shù)據(jù),多線程耗時1.67分鐘,單線程耗時5.75分鐘。通過對不同線程數(shù)的測試,發(fā)現(xiàn)不是線程數(shù)越多越好,具體多少合適,網(wǎng)上有一個不成文的算法:
CPU核心數(shù)量*2
+2 個線程。
附:測試電腦配置
![50c32c2a-6555-11ed-8abf-dac502259ad0.png](https://file1.elecfans.com//web2/M00/97/AF/wKgZomTnPbKAGglVAAE_quJ67SY626.png)
審核編輯 :李倩
-
SQL
+關(guān)注
關(guān)注
1文章
774瀏覽量
44250 -
多線程
+關(guān)注
關(guān)注
0文章
278瀏覽量
20071 -
spring
+關(guān)注
關(guān)注
0文章
340瀏覽量
14388 -
SpringBoot
+關(guān)注
關(guān)注
0文章
174瀏覽量
201
原文標(biāo)題:性能爆表:SpringBoot利用ThreadPoolTaskExecutor批量插入百萬級數(shù)據(jù)實測!
文章出處:【微信號:芋道源碼,微信公眾號:芋道源碼】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關(guān)推薦
工業(yè)現(xiàn)場數(shù)據(jù)實時采集:解鎖工業(yè)智能化轉(zhuǎn)型的關(guān)鍵
![工業(yè)現(xiàn)場<b class='flag-5'>數(shù)據(jù)實</b>時采集:解鎖工業(yè)智能化轉(zhuǎn)型的關(guān)鍵](https://file1.elecfans.com//web3/M00/06/A3/wKgZO2eN3heASBrwAAVkTcxNkUQ742.png)
基于Java、springboot、avue技術(shù)開發(fā)的醫(yī)院績效核算系統(tǒng)源碼
![基于Java、<b class='flag-5'>springboot</b>、avue技術(shù)開發(fā)的醫(yī)院績效核算系統(tǒng)源碼](https://file1.elecfans.com/web3/M00/05/AF/wKgZO2eCOdqAUrJ-AAT4hGtZ44o171.png)
ADS8674輸出數(shù)據(jù)有尖刺,實測時鐘時序和datasheet不符是怎么回事?
電桿傾斜監(jiān)測裝置 桿塔傾斜監(jiān)測裝置 支持數(shù)據(jù)實時讀取 精確預(yù)警
![電桿傾斜監(jiān)測裝置 桿塔傾斜監(jiān)測裝置 支持<b class='flag-5'>數(shù)據(jù)實</b>時讀取 精確預(yù)警](https://file1.elecfans.com/web2/M00/0B/C1/wKgaomcpiy-AL3MUAAJOKt47Smw316.png)
企業(yè)級數(shù)據(jù)庫的配置和管理要求匯總
DNA計算機(jī)研究取得突破性進(jìn)展:PB級數(shù)據(jù)存儲與高效處理
天拓四方:工業(yè)級數(shù)據(jù)采集網(wǎng)關(guān)核心功能解析與應(yīng)用價值
帶電插入檢測電路的TPD4S1394 Firewire ESD箝位電路數(shù)據(jù)表
![帶電<b class='flag-5'>插入</b>檢測電路的TPD4S1394 Firewire ESD箝位電路<b class='flag-5'>數(shù)據(jù)</b>表](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
LPS完成戰(zhàn)略性收購 增強(qiáng)數(shù)據(jù)實踐和營銷云能力
![LPS完成戰(zhàn)略性收購 增強(qiáng)<b class='flag-5'>數(shù)據(jù)實</b>踐和營銷云能力](https://file1.elecfans.com//web2/M00/EB/D1/wKgZomZf4aaAQ9_ZAAGF7IblHl0164.jpg)
fanuc robot interface V3.0批量讀寫數(shù)據(jù)寄存器問題
濾波器插入損耗怎么測試?測試標(biāo)準(zhǔn)是什么?
![濾波器<b class='flag-5'>插入</b>損耗怎么測試?測試標(biāo)準(zhǔn)是什么?](https://file1.elecfans.com//web2/M00/E5/F9/wKgaomZB7YGAFyBrAABbul4tX6A120.png)
淺談多線合用牽引變電所電能質(zhì)量實測分析
![淺談多線合用牽引變電所電能質(zhì)量<b class='flag-5'>實測</b>分析](https://file1.elecfans.com//web2/M00/C4/11/wKgZomXxAVmANuUWAAAbOt4a07k23.jpeg)
具有過流保護(hù)功能的LMG341xR050 600V 50mΩ集成式GaN功率級數(shù)據(jù)表
![具有過流保護(hù)功能的LMG341xR050 600V 50mΩ集成式GaN功率<b class='flag-5'>級數(shù)據(jù)</b>表](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
評論