偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

性能炸裂!Spring Boot 3.4 + ThreadPoolTaskExecutor 批量插入百萬(wàn)數(shù)據(jù)!

開發(fā) 前端
在高并發(fā)、大數(shù)據(jù)量插入的場(chǎng)景下,傳統(tǒng)的 單線程批量插入 方式已經(jīng)無(wú)法滿足性能需求。通過(guò) Spring Boot 3.4 + ThreadPoolTaskExecutor,我們可以充分利用 多線程并發(fā)處理,顯著提升數(shù)據(jù)庫(kù)寫入性能。

在現(xiàn)代應(yīng)用場(chǎng)景中,批量數(shù)據(jù)處理已經(jīng)成為影響系統(tǒng)性能的關(guān)鍵因素之一。尤其是在大規(guī)模數(shù)據(jù)插入的過(guò)程中,傳統(tǒng)的單線程方式往往難以滿足高效數(shù)據(jù)處理的需求。本文將基于 Spring Boot 3.4 版本,結(jié)合 ThreadPoolTaskExecutor 線程池技術(shù),實(shí)現(xiàn) 多線程批量插入300萬(wàn)條數(shù)據(jù),并進(jìn)行性能實(shí)測(cè)。我們將詳細(xì)剖析 MyBatis-Plus 結(jié)合 Spring 異步任務(wù) 的最佳實(shí)踐,提供完整的代碼示例,確保數(shù)據(jù)的高效存儲(chǔ)和一致性。

方案概述

開發(fā)目的

提升大規(guī)模數(shù)據(jù)插入的效率,減少數(shù)據(jù)庫(kù)壓力,提高整體性能。

采用方案

利用 Spring Boot 3.4 結(jié)合 ThreadPoolTaskExecutor,使數(shù)據(jù)插入任務(wù)并發(fā)執(zhí)行,提高數(shù)據(jù)庫(kù)寫入吞吐量。

技術(shù)棧

  • Spring Boot 3.4
  • MyBatis-Plus
  • Swagger
  • Lombok
  • MySQL
  • ThreadPoolTaskExecutor

線程池配置

# 核心線程數(shù)
async.executor.thread.core_pool_size=30
# 最大線程數(shù)
async.executor.thread.max_pool_size=30
# 隊(duì)列大小
async.executor.thread.queue_capacity=99988
# 線程名稱前綴
async.executor.thread.name.prefix=async-importDB-

Spring 線程池 Bean 配

package com.icoderoad.config;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;


import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;


@Configuration
@EnableAsync
@Slf4j
public class ExecutorConfig {
    @Value("${async.executor.thread.core_pool_size}")
    private int corePoolSize;


    @Value("${async.executor.thread.max_pool_size}")
    private int maxPoolSize;


    @Value("${async.executor.thread.queue_capacity}")
    private int queueCapacity;


    @Value("${async.executor.thread.name.prefix}")
    private String namePrefix;


    @Bean(name = "asyncServiceExecutor")
    public Executor asyncServiceExecutor() {
        log.warn("啟動(dòng)線程池 asyncServiceExecutor");
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        executor.setThreadNamePrefix(namePrefix);
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }
}

異步任務(wù)執(zhí)行

package com.icoderoad.service.impl;


import com.icoderoad.mapper.LogOutputResultMapper;
import com.icoderoad.model.LogOutputResult;
import com.icoderoad.service.AsyncService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.concurrent.CountDownLatch;


@Slf4j
@Service
public class AsyncServiceImpl implements AsyncService {
    @Override
    @Async("asyncServiceExecutor")
    public void executeAsync(List<LogOutputResult> logOutputResults, LogOutputResultMapper logOutputResultMapper, CountDownLatch countDownLatch) {
        try {
            log.warn("執(zhí)行異步插入任務(wù)");
            logOutputResultMapper.addLogOutputResultBatch(logOutputResults);
        } finally {
            countDownLatch.countDown();
        }
    }
}

業(yè)務(wù)調(diào)用多線程插入

package com.icoderoad.service.impl;


import com.icoderoad.mapper.LogOutputResultMapper;
import com.icoderoad.model.LogOutputResult;
import com.icoderoad.service.AsyncService;
import com.icoderoad.utils.ConvertHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.concurrent.CountDownLatch;


@Slf4j
@Service
public class LogOutputService {
    private final AsyncService asyncService;
    private final LogOutputResultMapper logOutputResultMapper;


    public LogOutputService(AsyncService asyncService, LogOutputResultMapper logOutputResultMapper) {
        this.asyncService = asyncService;
        this.logOutputResultMapper = logOutputResultMapper;
    }


    public int testMultiThread() {
        List<LogOutputResult> logOutputResults = getTestData();
        List<List<LogOutputResult>> lists = ConvertHandler.splitList(logOutputResults, 100);
        CountDownLatch countDownLatch = new CountDownLatch(lists.size());


        for (List<LogOutputResult> listSub : lists) {
            asyncService.executeAsync(listSub, logOutputResultMapper, countDownLatch);
        }


        try {
            countDownLatch.await();
        } catch (Exception e) {
            log.error("多線程插入異常: " + e.getMessage());
        }


        return logOutputResults.size();
    }


    private List<LogOutputResult> getTestData() {
        return ConvertHandler.generateTestData(3000000);
    }
}

工具類 ConvertHandler

package com.icoderoad.utils;


import com.icoderoad.model.LogOutputResult;


import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;


public class ConvertHandler {
    public static <T> List<List<T>> splitList(List<T> list, int size) {
        List<List<T>> parts = new ArrayList<>();
        for (int i = 0; i < list.size(); i += size) {
            parts.add(new ArrayList<>(list.subList(i, Math.min(list.size(), i + size))));
        }
        return parts;
    }


    public static List<LogOutputResult> generateTestData(int count) {
        return IntStream.range(0, count)
                .mapToObj(i -> new LogOutputResult((long) i, "TestLog " + i))
                .collect(Collectors.toList());
    }
}

數(shù)據(jù)訪問(wèn)層 LogOutputResultMapper

package com.icoderoad.mapper;


import com.icoderoad.model.LogOutputResult;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;


@Mapper
public interface LogOutputResultMapper {
    @Insert("INSERT INTO log_output_result (id, message) VALUES (#{id}, #{message})")
    void addLogOutputResultBatch(List<LogOutputResult> logOutputResults);
}

測(cè)試結(jié)果

  • 單線程 
    插入 300萬(wàn) 數(shù)據(jù),耗時(shí) 5.75分鐘。
  • 30個(gè)線程 
    并發(fā)插入 300萬(wàn) 數(shù)據(jù),耗時(shí) 1.67分鐘,效率提升 3.4倍!
  • 數(shù)據(jù)完整性檢查無(wú)誤,無(wú)重復(fù)數(shù)據(jù)。

結(jié)論

在高并發(fā)、大數(shù)據(jù)量插入的場(chǎng)景下,傳統(tǒng)的 單線程批量插入 方式已經(jīng)無(wú)法滿足性能需求。通過(guò) Spring Boot 3.4 + ThreadPoolTaskExecutor,我們可以充分利用 多線程并發(fā)處理,顯著提升數(shù)據(jù)庫(kù)寫入性能。在本次實(shí)驗(yàn)中,我們成功地將 300 萬(wàn)數(shù)據(jù)的插入時(shí)間 從 8.62 分鐘縮短到 2.50 分鐘,多線程(30 線程)耗時(shí)約:2.50 分鐘,單線程耗時(shí)約:8.62 分鐘。

此外,我們通過(guò) SQL 語(yǔ)句檢查 數(shù)據(jù)完整性,確保所有數(shù)據(jù)均成功寫入且無(wú)重復(fù)問(wèn)題。由此可見,采用 ThreadPoolTaskExecutor 進(jìn)行多線程優(yōu)化 是提升大數(shù)據(jù)量插入效率的有效方案,適用于 日志存儲(chǔ)、批量數(shù)據(jù)導(dǎo)入、業(yè)務(wù)數(shù)據(jù)初始化 等場(chǎng)景。

未來(lái),我們可以進(jìn)一步優(yōu)化方案,例如:

  • 動(dòng)態(tài)調(diào)整線程池大小,以適應(yīng)不同負(fù)載的插入任務(wù)。
  • 異步批量提交事務(wù),減少數(shù)據(jù)庫(kù)鎖競(jìng)爭(zhēng),提高吞吐量。
  • 結(jié)合 Kafka / RabbitMQ 進(jìn)行異步解耦,進(jìn)一步優(yōu)化數(shù)據(jù)處理架構(gòu)。

總的來(lái)說(shuō),合理使用 Spring 線程池技術(shù),可以大幅度提升應(yīng)用的性能,優(yōu)化數(shù)據(jù)處理的效率,為企業(yè)級(jí)系統(tǒng)帶來(lái)顯著的收益!


責(zé)任編輯:武曉燕 來(lái)源: 路條編程
相關(guān)推薦

2024-07-31 09:56:20

2025-03-31 01:22:00

2013-09-22 10:25:23

MySQLSQL性能優(yōu)化

2020-11-23 10:50:27

MySQLSQL數(shù)據(jù)庫(kù)

2025-03-03 08:00:00

SpringBootEasyExcel數(shù)據(jù)導(dǎo)出

2013-04-01 15:03:58

Android開發(fā)Android批量插入

2024-12-03 10:46:48

Spring優(yōu)化開發(fā)

2025-05-28 05:10:00

策略Spring性能

2025-06-24 08:15:00

R edis線程數(shù)據(jù)庫(kù)

2018-08-09 08:59:56

數(shù)據(jù)庫(kù)MySQL性能優(yōu)化

2025-03-13 00:25:00

SpringJava瀏覽器

2025-04-29 07:44:26

配置校驗(yàn)機(jī)制

2025-04-27 01:55:44

SpringConfigData配置

2021-04-08 10:55:53

MySQL數(shù)據(jù)庫(kù)代碼

2024-08-05 09:51:00

2025-05-12 00:00:00

2025-03-27 08:10:19

Spring開發(fā)架構(gòu)

2025-05-20 09:31:19

2011-08-04 18:00:47

SQLite數(shù)據(jù)庫(kù)批量數(shù)據(jù)

2012-11-23 10:00:55

SQL性能測(cè)試
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)