demand: TMS系统 - 运输管理 - 通过多线程分批处理,优化漏组批次订单一次性查询/导出10万+数据的效率

committer: heyu
This commit is contained in:
aike 2025-09-13 11:48:23 +08:00
parent 030c9fc475
commit 68f4b3fb4d

View File

@ -79,26 +79,26 @@ public class EmisTmsSiteBatchMissServiceImpl extends EmisBaseService implements
log.info("Selecting TMS site batch miss list with params: {}", emisTmsSiteBatchMiss); log.info("Selecting TMS site batch miss list with params: {}", emisTmsSiteBatchMiss);
// 检查是否需要多线程优化 // 检查是否需要多线程优化
// 通过检查请求参数中是否包含大分页标识 // 通过PageHelper获取当前分页信息
boolean useMultiThread = false; boolean useMultiThread = false;
Integer pageSize = null; Integer pageSize = null;
Integer pageNum = null; Integer pageNum = null;
// 尝试从params中获取分页参数 try {
if (emisTmsSiteBatchMiss.getParams() != null) { // 从PageHelper的ThreadLocal中获取分页信息
Object pageSizeObj = emisTmsSiteBatchMiss.getParams().get("pageSize"); com.github.pagehelper.Page<?> localPage = PageHelper.getLocalPage();
Object pageNumObj = emisTmsSiteBatchMiss.getParams().get("pageNum"); if (localPage != null) {
pageSize = localPage.getPageSize();
pageNum = localPage.getPageNum();
if (pageSizeObj instanceof Integer) { if (pageSize != null && pageSize > 1000) {
pageSize = (Integer) pageSizeObj;
if (pageSize > 1000) {
useMultiThread = true; useMultiThread = true;
log.info("Detected large pageSize: {}, pageNum: {}, will use multi-thread optimization", pageSize,
pageNum);
} }
} }
} catch (Exception e) {
if (pageNumObj instanceof Integer) { log.warn("Failed to get page info from PageHelper: {}", e.getMessage());
pageNum = (Integer) pageNumObj;
}
} }
// 如果pageSize > 1000,使用多线程查询优化 // 如果pageSize > 1000,使用多线程查询优化
@ -192,8 +192,7 @@ public class EmisTmsSiteBatchMissServiceImpl extends EmisBaseService implements
try { try {
// 为每个线程分配查询任务 // 为每个线程分配查询任务
for (int i = 0; i < threadCount; i++) { for (int i = 0; i < threadCount; i++) {
int start = i * RECORDS_PER_THREAD; final int threadIndex = i;
int end = Math.min(start + RECORDS_PER_THREAD, pageSize);
// 创建查询任务 // 创建查询任务
Callable<List<EmisTmsSiteBatchMiss>> task = () -> { Callable<List<EmisTmsSiteBatchMiss>> task = () -> {
@ -201,20 +200,15 @@ public class EmisTmsSiteBatchMissServiceImpl extends EmisBaseService implements
// 创建查询参数的副本,避免线程间冲突 // 创建查询参数的副本,避免线程间冲突
EmisTmsSiteBatchMiss queryParam = createQueryParamCopy(emisTmsSiteBatchMiss); EmisTmsSiteBatchMiss queryParam = createQueryParamCopy(emisTmsSiteBatchMiss);
// 设置分页参数 // 设置分页参数 - 每个线程查询1000条记录
PageHelper.startPage(1, end - start); PageHelper.startPage(1, RECORDS_PER_THREAD);
// 执行查询 // 执行查询
List<EmisTmsSiteBatchMiss> result = emisTmsSiteBatchMissMapper List<EmisTmsSiteBatchMiss> result = emisTmsSiteBatchMissMapper
.selectEmisTmsSiteBatchMissList(queryParam); .selectEmisTmsSiteBatchMissList(queryParam);
// 只返回当前线程负责的数据范围 // 返回查询结果,不需要手动截取,因为PageHelper已经处理了分页
if (result.size() > start) { return result != null ? result : new ArrayList<>();
int actualEnd = Math.min(start + (end - start), result.size());
return result.subList(start, actualEnd);
}
return new ArrayList<>();
} catch (Exception e) { } catch (Exception e) {
log.error("Error in thread query task", e); log.error("Error in thread query task", e);
return new ArrayList<>(); return new ArrayList<>();