You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
MongoJobExecutionDao.findRunningJobExecutions() issues N+1 MongoDB queries, where N is the total number of JobInstance records for a given job name.
The current implementation iterates over every JobInstance and issues a separate MongoDB query for each one:
for (JobInstancejobInstance : jobInstances) {
Queryquery = query(
where("jobInstanceId").is(jobInstance.getId()).and("status").in("STARTING", "STARTED", "STOPPING"));
this.mongoOperations.find(query, ...); // called N times
}
For a job with 1,000 historical instances but only 1 running execution, this results in 1,001 MongoDB queries.
In contrast, the JDBC implementation uses a single JOIN query:
SELECTE.JOB_EXECUTION_IDFROM BATCH_JOB_EXECUTION E, BATCH_JOB_INSTANCE I
WHEREE.JOB_INSTANCE_ID=I.JOB_INSTANCE_IDANDI.JOB_NAME=? ANDE.STATUSIN ('STARTING', 'STARTED', 'STOPPING')
Environment
Spring Batch version: main branch (6.x)
Database: MongoDB
Expected behavior
The method should use a single batch query with $in operator instead of N individual queries:
Map<Long, JobInstance> jobInstanceMap = newHashMap<>();
for (JobInstancejobInstance : jobInstances) {
jobInstanceMap.put(jobInstance.getId(), jobInstance);
}
Queryquery = query(
where("jobInstanceId").in(jobInstanceMap.keySet()).and("status").in("STARTING", "STARTED", "STOPPING"));
this.mongoOperations.find(query, ...); // called once
This reduces the query count from 1+N to 2, regardless of how many historical job instances exist.
Activity
spring-projectsGH-5356: Optimize findRunningJobExecutions to use batc…
Optimize findRunningJobExecutions to use batch query