diff --git a/src/main/java/org/tdl/vireo/model/repo/impl/SubmissionRepoImpl.java b/src/main/java/org/tdl/vireo/model/repo/impl/SubmissionRepoImpl.java index 79fb337c0..29a6897b6 100644 --- a/src/main/java/org/tdl/vireo/model/repo/impl/SubmissionRepoImpl.java +++ b/src/main/java/org/tdl/vireo/model/repo/impl/SubmissionRepoImpl.java @@ -449,6 +449,7 @@ public int compare(SubmissionListColumn svc1, SubmissionListColumn svc2) { sqlAliasBuilders.add("s.id"); int n = 0; + int totalFieldValueConditions = 0; for (SubmissionListColumn submissionListColumn : allSubmissionListColumns) { @@ -552,6 +553,7 @@ public int compare(SubmissionListColumn svc1, SubmissionListColumn svc2) { } if (sqlBuilder.length() > 0) { + totalFieldValueConditions++; sqlWhereBuilderList.add(sqlBuilder); if (!sqlCountWherePredicate.containsKey(predicateId)) { @@ -942,7 +944,6 @@ public int compare(SubmissionListColumn svc1, SubmissionListColumn svc2) { }); sqlSelectBuilder.setLength(sqlSelectBuilder.length() - 2); sqlSelectBuilder.append(" FROM submission s"); - sqlCountSelectBuilder.insert(0, "SELECT COUNT(DISTINCT s.id) FROM submission s"); // if ordering, complete order by clause and strip the tailing comma if (sqlOrderBysBuilder.length() > 0) { @@ -1036,7 +1037,14 @@ public int compare(SubmissionListColumn svc1, SubmissionListColumn svc2) { } String sqlQuery = sqlSelectBuilder.toString() + sqlJoinsBuilder.toString() + sqlBuilder.toString(); - String sqlCountQuery = sqlCountSelectBuilder.toString(); + String sqlCountQuery = "SELECT COUNT(DISTINCT s.id) FROM submission s"; + + // Use count query optimization only when there are fewer than 2 field values in the where clause. + if (totalFieldValueConditions > 1) { + sqlCountQuery += sqlJoinsBuilder.toString() + sqlBuilder.toString(); + } else { + sqlCountQuery += sqlCountSelectBuilder.toString(); + } if (pageable != null) { // determine the offset and limit of the query