前言
- 開發(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)行測試

多線程 測試 2000003 耗時如下:耗時1.67分鐘


本次開啟30個線程,截圖如下:

單線程測試2000003 耗時如下:耗時5.75分鐘


檢查多線程入庫的數(shù)據(jù),檢查是否存在重復(fù)入庫的問題:
根據(jù)id分組,查看是否有id重復(fù)的數(shù)據(jù),通過sql語句檢查,沒有發(fā)現(xiàn)重復(fù)入庫的問題

檢查數(shù)據(jù)完整性:通過sql語句查詢,多線程錄入數(shù)據(jù)完整

測試結(jié)果
不同線程數(shù)測試:


總結(jié)
通過以上測試案列,同樣是導(dǎo)入2000003 條數(shù)據(jù),多線程耗時1.67分鐘,單線程耗時5.75分鐘。通過對不同線程數(shù)的測試,發(fā)現(xiàn)不是線程數(shù)越多越好,具體多少合適,網(wǎng)上有一個不成文的算法:
CPU核心數(shù)量*2 +2 個線程。
附:測試電腦配置

審核編輯 :李倩
-
SQL
+關(guān)注
關(guān)注
1文章
789瀏覽量
46700 -
多線程
+關(guān)注
關(guān)注
0文章
279瀏覽量
21027 -
spring
+關(guān)注
關(guān)注
0文章
341瀏覽量
15936 -
SpringBoot
+關(guān)注
關(guān)注
0文章
177瀏覽量
688
原文標(biāo)題:性能爆表:SpringBoot利用ThreadPoolTaskExecutor批量插入百萬級數(shù)據(jù)實測!
文章出處:【微信號:芋道源碼,微信公眾號:芋道源碼】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
中軟國際中標(biāo)國際金融巨頭千萬級數(shù)據(jù)平臺建設(shè)項目
固件燒錄速度實測:JTAG比UART快6.8倍
超聲波風(fēng)速傳感器硬核評測:精度 / 響應(yīng)速度 / 功耗數(shù)據(jù)實測
某化工企業(yè)實測:30 天部署采集系統(tǒng),數(shù)據(jù)同步零延遲
如何使用SpringBoot、Vue2.0、MySQL開發(fā)一套云診所系統(tǒng)?
10G通信信號衰減難題破解:沃虎片式網(wǎng)絡(luò)變壓器實測案例
MR100A工業(yè)4G路由器模塊深度評測:4G聯(lián)網(wǎng)與工業(yè)級數(shù)據(jù)透傳的完美融合
Flutter 移動端開發(fā):集成淘寶 API 實現(xiàn)商品數(shù)據(jù)實時展示 APP
醫(yī)院隨訪管理系統(tǒng)源碼,三級隨訪系統(tǒng)源碼,Java+Springboot,Vue,Ant-Design+MySQL5
偉創(chuàng)力重磅發(fā)布全球首款面向千兆瓦級數(shù)據(jù)中心的AI基礎(chǔ)設(shè)施平臺
掌握NFC標(biāo)簽批量讀取,輕松提升數(shù)據(jù)采集效率!
RK3506開發(fā)板Xenomai內(nèi)核RT-Linux實時性系統(tǒng)適配教程與性能實測,實測僅7μs穩(wěn)定延時
復(fù)雜裝備研發(fā)設(shè)計中利用數(shù)據(jù)實現(xiàn)大規(guī)模個性化定制
樹莓派5 + Hailo AI加速器:工業(yè)級數(shù)值數(shù)據(jù)處理實戰(zhàn),打通SQLite與機(jī)器學(xué)習(xí)全鏈路
SpringBoot利用ThreadPoolTaskExecutor批量插入百萬級數(shù)據(jù)實測!
評論