一、DxxJxx系统
1. 原代码
@GetMapping("/index")
public AjaxResult index() {
JSONObject data = new JSONObject();
PartyMember mquery = new PartyMember();
mquery.setDelFlag(0);
List<PartyMember> list = partyMemberService.selectPartyMemberList(mquery);
if (CollectionUtils.isNotEmpty(list)) {
//计算数据
//政治面貌分布
Map politicalStatusMap = getPoliticalStatusMap(list);
data.put("politicalStatusCount", coverMapToList(politicalStatusMap));
//性别分布
Map sexMap = getSexMap(list);
data.put("sexCount", coverMapToList(sexMap));
//年龄分布
Map ageMap = getAgeMap(list);
data.put("ageCount", coverMapToList(ageMap));
//教育程度分布
Map educationLevelMap = getEducationLevelMap(list);
data.put("educationLevelCount", coverMapToList(educationLevelMap));
//转正情况分布
Map regularStatusMap = getRegularStatusMap(list);
data.put("regularStatusCount", coverMapToList(regularStatusMap));
//民族分布
Map nationalityMap = getNationalityMap(list);
data.put("nationalityCount", coverMapToList(nationalityMap));
}
PartyBranch bquery = new PartyBranch();
bquery.setDelFlag(0);
List<PartyBranch> listB = partyBranchService.selectPartyBranchList(bquery);
if (CollectionUtils.isNotEmpty(listB)) {
//党支部数
data.put("branchNum", listB.size());
//党员总数
data.put("memberTotalNum", listB.stream().mapToInt(PartyBranch::getMemberNum).sum());
//区域分布
data.put("areaCount", getAreaList(listB));
//党组织所属阶段
data.put("branchBelongLevelCount", coverMapToList(getBelongLevelMap(listB)));
//党组织成立时间
data.put("branchEstablishDateCount", coverMapToList(getEstablishDateMap(listB)));
}
return AjaxResult.success(data);
}
2. 优化后v1
@GetMapping("/index/test")
public AjaxResult indexTest() throws ExecutionException, InterruptedException {
JSONObject data = new JSONObject();
ExecutorService executorService = Executors.newFixedThreadPool(2);
Future<?> future1 = executorService.submit(() -> {
PartyMember mquery = new PartyMember();
mquery.setDelFlag(0);
List<PartyMember> list = partyMemberService.selectPartyMemberList(mquery);
if (CollectionUtils.isNotEmpty(list)) {
//计算数据
//政治面貌分布
Map politicalStatusMap = getPoliticalStatusMap(list);
data.put("politicalStatusCount", coverMapToList(politicalStatusMap));
//性别分布
Map sexMap = getSexMap(list);
data.put("sexCount", coverMapToList(sexMap));
//年龄分布
Map ageMap = getAgeMap(list);
data.put("ageCount", coverMapToList(ageMap));
//教育程度分布
Map educationLevelMap = getEducationLevelMap(list);
data.put("educationLevelCount", coverMapToList(educationLevelMap));
//转正情况分布
Map regularStatusMap = getRegularStatusMap(list);
data.put("regularStatusCount", coverMapToList(regularStatusMap));
//民族分布
Map nationalityMap = getNationalityMap(list);
data.put("nationalityCount", coverMapToList(nationalityMap));
}
});
Future<?> future2 = executorService.submit(() -> {
PartyBranch bquery = new PartyBranch();
bquery.setDelFlag(0);
List<PartyBranch> listB = partyBranchService.selectPartyBranchList(bquery);
if (CollectionUtils.isNotEmpty(listB)) {
//党支部数
data.put("branchNum", listB.size());
//党员总数
data.put("memberTotalNum", listB.stream().mapToInt(PartyBranch::getMemberNum).sum());
//区域分布
data.put("areaCount", getAreaList(listB));
//党组织所属阶段
data.put("branchBelongLevelCount", coverMapToList(getBelongLevelMap(listB)));
//党组织成立时间
data.put("branchEstablishDateCount", coverMapToList(getEstablishDateMap(listB)));
}
});
future1.get();
future2.get();
executorService.shutdown();
return AjaxResult.success(data);
}
2. 优化后v2
@GetMapping("/index/test2")
public AjaxResult indexTest2() throws ExecutionException, InterruptedException {
JSONObject data = new JSONObject();
ExecutorService executorService = Executors.newFixedThreadPool(13);
List<Future<?>> futures = new ArrayList<>();
Future<?> future1;
future1 = executorService.submit(() -> {
PartyMember mquery = new PartyMember();
mquery.setDelFlag(0);
List<PartyMember> list = partyMemberService.selectPartyMemberList(mquery);
if (CollectionUtils.isNotEmpty(list)) {
//计算数据
//政治面貌分布
futures.add(executorService.submit(() -> {
Map politicalStatusMap = getPoliticalStatusMap(list);
data.put("politicalStatusCount", coverMapToList(politicalStatusMap));
}));
//性别分布
futures.add(executorService.submit(() -> {
Map sexMap = getSexMap(list);
data.put("sexCount", coverMapToList(sexMap));
}));
//年龄分布
futures.add(executorService.submit(() -> {
Map ageMap = getAgeMap(list);
data.put("ageCount", coverMapToList(ageMap));
}));
//教育程度分布
futures.add(executorService.submit(() -> {
Map educationLevelMap = getEducationLevelMap(list);
data.put("educationLevelCount", coverMapToList(educationLevelMap));
}));
//转正情况分布
futures.add(executorService.submit(() -> {
Map regularStatusMap = getRegularStatusMap(list);
data.put("regularStatusCount", coverMapToList(regularStatusMap));
}));
//民族分布
futures.add(executorService.submit(() -> {
Map nationalityMap = getNationalityMap(list);
data.put("nationalityCount", coverMapToList(nationalityMap));
}));
}
});
Future<?> future2;
future2 = executorService.submit(() -> {
PartyBranch bquery = new PartyBranch();
bquery.setDelFlag(0);
List<PartyBranch> listB = partyBranchService.selectPartyBranchList(bquery);
if (CollectionUtils.isNotEmpty(listB)) {
//党支部数
futures.add(executorService.submit(() -> {
data.put("branchNum", listB.size());
}));
//党员总数
futures.add(executorService.submit(() -> {
data.put("memberTotalNum", listB.stream().mapToInt(PartyBranch::getMemberNum).sum());
}));
//区域分布
futures.add(executorService.submit(() -> {
data.put("areaCount", getAreaList(listB));
}));
//党组织所属阶段
futures.add(executorService.submit(() -> {
data.put("branchBelongLevelCount", coverMapToList(getBelongLevelMap(listB)));
}));
//党组织成立时间
futures.add(executorService.submit(() -> {
data.put("branchEstablishDateCount", coverMapToList(getEstablishDateMap(listB)));
}));
}
});
future1.get();
future2.get();
for (Future<?> future : futures) {
try {
future.get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
}
executorService.shutdown();
return AjaxResult.success(data);
}
二、JxxJxxBxxTxxKxx系统
1. 原代码
@GetMapping("/process")
public R<Map<String, Long>> recommendCompProcess() {
// 实名认证
CompBase queryCompBase = new CompBase();
queryCompBase.setCompType(1);
List<CompBase> compBaseList = compBaseService.selectCompBaseNewList(queryCompBase);
Long authComp = compBaseList.stream().filter(comp -> comp.getIsAuth() != 0 && comp.getStatus() == 0).count();
// 信息填报
Long fillInfo = compBaseList.stream().filter(comp -> comp.getStatus() == -1 || comp.getStatus() == 1).count();
Integer baseComp = compBaseFinalMapper.countCompByFlagAndType(1, 1, null);
Integer qualityComp = compBaseFinalMapper.countCompByFlagAndType(3, 1,null);
// 待入库
Integer compImportCount = compBaseFinalMapper.countComp(null, 6);
Map<String, Long> resultMap = new HashMap<>();
// 待入库
resultMap.put("compImportCount", Long.valueOf(compImportCount));
// 实名认证
resultMap.put("authComp", authComp);
// 信息完善
resultMap.put("fillInfo", fillInfo);
// 基础库
resultMap.put("baseComp", Long.valueOf(baseComp));
// 专家评审
//resultMap.put("waitingReview", waitingReview);
// 精选库
resultMap.put("qualityComp", Long.valueOf(qualityComp));
return R.ok(resultMap);
}
2.优化后
上面直接使用stream过滤数据,相当于遍历,由于compBaseList有几万条数据,导致太慢了。后面改成用sql条件从库里查了,也就是从一次与数据库交互和两个stream直接改成两次与数据库交互。改完之后快多了,再使用多线程查询的话有点多余了,但是后面如果数据量再增加的话,使用多线程异步查询效率提高会明显很多。
@GetMapping("/process")
public R<Map<String, Long>> recommendCompProcess() {
Map<String, Long> resultMap = new HashMap<>();
ExecutorService executorService = Executors.newFixedThreadPool(5);
Future<?> future1 = executorService.submit(() -> {
// 实名认证
CompBase queryAuthCompBase = new CompBase();
queryAuthCompBase.setCompType(1);
queryAuthCompBase.setIsAuthList(Arrays.asList(1, 2, -1));
queryAuthCompBase.setStatus(0);
List<CompBase> authComp = compBaseService.selectCompBaseNewList(queryAuthCompBase);
resultMap.put("authComp", (long) authComp.size());
});
Future<?> future2 = executorService.submit(() -> {
// 信息填报
CompBase queryFillInfoCompBase = new CompBase();
queryFillInfoCompBase.setCompType(1);
queryFillInfoCompBase.setStatusList(Arrays.asList(-1, 1));
List<CompBase> fillInfo = compBaseService.selectCompBaseNewList(queryFillInfoCompBase);
resultMap.put("fillInfo", (long) fillInfo.size());
});
Future<?> future3 = executorService.submit(() -> {
// 基础库
Integer baseComp = compBaseFinalMapper.countCompByFlagAndType(1, 1, null);
resultMap.put("baseComp", Long.valueOf(baseComp));
});
Future<?> future4 = executorService.submit(() -> {
// 精选库
Integer qualityComp = compBaseFinalMapper.countCompByFlagAndType(3, 1, null);
resultMap.put("qualityComp", Long.valueOf(qualityComp));
});
Future<?> future5 = executorService.submit(() -> {
// 待入库
Integer compImportCount = compBaseFinalMapper.countComp(null, 6);
resultMap.put("compImportCount", Long.valueOf(compImportCount));
});
try {
future1.get();
future2.get();
future3.get();
future4.get();
future5.get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
executorService.shutdown();
return R.ok(resultMap);
}