From b7339c2e9641c2ac4428fa059d6faa6b03243e6f Mon Sep 17 00:00:00 2001 From: Jimmy Praet Date: Fri, 6 Sep 2024 23:25:08 +0200 Subject: [PATCH] Move ORDER BY in getLastStepExecution from DB to java This addresses performance issues with large STEP_EXECUTION table on DB2. Fixes #4657 --- .../batch/core/repository/dao/JdbcStepExecutionDao.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcStepExecutionDao.java b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcStepExecutionDao.java index 5c53a6f4a0..c086ec3b01 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcStepExecutionDao.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcStepExecutionDao.java @@ -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. @@ -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; @@ -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 = """ @@ -117,6 +117,10 @@ SELECT COUNT(*) WHERE STEP_EXECUTION_ID = ? """; + private static final Comparator 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; @@ -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; }