Skip to content

GH-5356: Optimize findRunningJobExecutions to use batch query#5357

Open
Wordbe wants to merge 1 commit intospring-projects:mainfrom
Wordbe:GH-5356
Open

GH-5356: Optimize findRunningJobExecutions to use batch query#5357
Wordbe wants to merge 1 commit intospring-projects:mainfrom
Wordbe:GH-5356

Conversation

@Wordbe
Copy link
Contributor

@Wordbe Wordbe commented Mar 24, 2026

Summary

MongoJobExecutionDao.findRunningJobExecutions() issues N+1 MongoDB queries, where N is the total number of job instances for a given job name.

This change replaces individual per-instance queries with a single batch query using the $in operator.

Before: 1 query (load all instances) + N queries (one per instance) = 1+N queries
After: 1 query (load all instances) + 1 batch query ($in) = 2 queries

Changes

  • Build a Map<Long, JobInstance> from loaded instances for O(1) lookup
  • Use $in(jobInstanceMap.keySet()) to find all running executions in a single query
  • Add early return for empty instances
  • Add integration test for running executions across multiple job instances

Closes #5356


  • Rebase on latest main and squash commits
  • Add/Update unit tests as needed
  • Sign-off commits according to the DCO

…h query

Replace N+1 MongoDB queries with a single batch query using
the $in operator in MongoJobExecutionDao.findRunningJobExecutions().

Signed-off-by: Wordbe <seonghojin3@gmail.com>
if (jobInstances.isEmpty()) {
return Collections.emptySet();
}
Map<Long, JobInstance> jobInstanceMap = new HashMap<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use streams here.

Map<Long, JobInstance> jobInstanceMap = jobInstances.stream().collect(Collectors.toMap(JobInstance::getId, Function.identity()));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MongoJobExecutionDao.findRunningJobExecutions issues N+1 MongoDB queries

2 participants