Skip to content

Commit 5a62de9

Browse files
jpraetfmbenhassine
authored andcommitted
Move ORDER BY in getLastStepExecution from DB to java
This addresses performance issues with large STEP_EXECUTION table on DB2. Fixes #4657
1 parent 196f457 commit 5a62de9

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcStepExecutionDao.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2023 the original author or authors.
2+
* Copyright 2006-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424
import java.util.ArrayList;
2525
import java.util.Arrays;
2626
import java.util.Collection;
27+
import java.util.Comparator;
2728
import java.util.Iterator;
2829
import java.util.List;
2930
import java.util.concurrent.locks.Lock;
@@ -97,7 +98,6 @@ public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao implement
9798
FROM %PREFIX%JOB_EXECUTION JE
9899
JOIN %PREFIX%STEP_EXECUTION SE ON SE.JOB_EXECUTION_ID = JE.JOB_EXECUTION_ID
99100
WHERE JE.JOB_INSTANCE_ID = ? AND SE.STEP_NAME = ?
100-
ORDER BY SE.CREATE_TIME DESC, SE.STEP_EXECUTION_ID DESC
101101
""";
102102

103103
private static final String CURRENT_VERSION_STEP_EXECUTION = """
@@ -117,6 +117,10 @@ SELECT COUNT(*)
117117
WHERE STEP_EXECUTION_ID = ?
118118
""";
119119

120+
private static final Comparator<StepExecution> BY_CREATE_TIME_DESC_ID_DESC = Comparator
121+
.comparing(StepExecution::getCreateTime, Comparator.reverseOrder())
122+
.thenComparing(StepExecution::getId, Comparator.reverseOrder());
123+
120124
private int exitMessageLength = DEFAULT_EXIT_MESSAGE_LENGTH;
121125

122126
private DataFieldMaxValueIncrementer stepExecutionIncrementer;
@@ -348,6 +352,7 @@ public StepExecution getLastStepExecution(JobInstance jobInstance, String stepNa
348352
jobExecution.setVersion(rs.getInt(27));
349353
return new StepExecutionRowMapper(jobExecution).mapRow(rs, rowNum);
350354
}, jobInstance.getInstanceId(), stepName);
355+
executions.sort(BY_CREATE_TIME_DESC_ID_DESC);
351356
if (executions.isEmpty()) {
352357
return null;
353358
}

0 commit comments

Comments
 (0)