From bc995752d6b52f2f94bf17ea806c60f8d63b7342 Mon Sep 17 00:00:00 2001 From: mgduoduo Date: Wed, 15 Dec 2021 10:53:52 +0800 Subject: [PATCH] [DS-7256][refactor] Optimization for task instances query (#7331) - assemble a Map outside of the loop instead of the query inside Co-authored-by: gq47193 --- .../api/service/impl/TaskInstanceServiceImpl.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/TaskInstanceServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/TaskInstanceServiceImpl.java index 64ba304d44..ab53c58878 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/TaskInstanceServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/TaskInstanceServiceImpl.java @@ -42,6 +42,7 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -137,12 +138,13 @@ public class TaskInstanceServiceImpl extends BaseServiceImpl implements TaskInst exclusionSet.add(Constants.CLASS); exclusionSet.add("taskJson"); List taskInstanceList = taskInstanceIPage.getRecords(); - + List executorIds = taskInstanceList.stream().map(TaskInstance::getExecutorId).distinct().collect(Collectors.toList()); + List users = usersService.queryUser(executorIds); + Map userMap = users.stream().collect(Collectors.toMap(User::getId, v -> v)); for (TaskInstance taskInstance : taskInstanceList) { taskInstance.setDuration(DateUtils.format2Duration(taskInstance.getStartTime(), taskInstance.getEndTime())); - User executor = usersService.queryUser(taskInstance.getExecutorId()); - if (null != executor) { - taskInstance.setExecutorName(executor.getUserName()); + if (userMap.containsKey(taskInstance.getExecutorId())) { + taskInstance.setExecutorName(userMap.get(taskInstance.getExecutorId()).getUserName()); } } pageInfo.setTotal((int) taskInstanceIPage.getTotal());