Skip to content

Commit

Permalink
Move ORDER BY in getLastStepExecution from DB to java
Browse files Browse the repository at this point in the history
This addresses performance issues with large STEP_EXECUTION table on DB2.

Fixes #4657
  • Loading branch information
jpraet authored and fmbenhassine committed Sep 19, 2024
1 parent 196f457 commit 5a62de9
Showing 1 changed file with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2023 the original author or authors.
* Copyright 2006-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.locks.Lock;
Expand Down Expand Up @@ -97,7 +98,6 @@ public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao implement
FROM %PREFIX%JOB_EXECUTION JE
JOIN %PREFIX%STEP_EXECUTION SE ON SE.JOB_EXECUTION_ID = JE.JOB_EXECUTION_ID
WHERE JE.JOB_INSTANCE_ID = ? AND SE.STEP_NAME = ?
ORDER BY SE.CREATE_TIME DESC, SE.STEP_EXECUTION_ID DESC
""";

private static final String CURRENT_VERSION_STEP_EXECUTION = """
Expand All @@ -117,6 +117,10 @@ SELECT COUNT(*)
WHERE STEP_EXECUTION_ID = ?
""";

private static final Comparator<StepExecution> BY_CREATE_TIME_DESC_ID_DESC = Comparator
.comparing(StepExecution::getCreateTime, Comparator.reverseOrder())
.thenComparing(StepExecution::getId, Comparator.reverseOrder());

private int exitMessageLength = DEFAULT_EXIT_MESSAGE_LENGTH;

private DataFieldMaxValueIncrementer stepExecutionIncrementer;
Expand Down Expand Up @@ -348,6 +352,7 @@ public StepExecution getLastStepExecution(JobInstance jobInstance, String stepNa
jobExecution.setVersion(rs.getInt(27));
return new StepExecutionRowMapper(jobExecution).mapRow(rs, rowNum);
}, jobInstance.getInstanceId(), stepName);
executions.sort(BY_CREATE_TIME_DESC_ID_DESC);
if (executions.isEmpty()) {
return null;
}
Expand Down

0 comments on commit 5a62de9

Please sign in to comment.