Skip to content

Commit

Permalink
Revert "improve query performance (#17862)"
Browse files Browse the repository at this point in the history
This reverts commit e0db09b.
  • Loading branch information
malikdiarra committed Oct 13, 2022
1 parent 3c147fb commit 3f85fc4
Showing 1 changed file with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ Map<JobStatus, Double> overallJobRuntimeForTerminalJobsInLastHour() {
final var query = """
SELECT status, extract(epoch from age(updated_at, created_at)) AS sec FROM jobs
WHERE updated_at >= NOW() - INTERVAL '1 HOUR'
AND jobs.status IN ('failed', 'succeeded', 'cancelled');
AND (jobs.status = 'failed' OR jobs.status = 'succeeded' OR jobs.status = 'cancelled');
""";
final var queryResults = ctx.fetch(query);
final var statuses = queryResults.getValues("status", JobStatus.class);
Expand All @@ -159,17 +159,32 @@ SELECT status, extract(epoch from age(updated_at, created_at)) AS sec FROM jobs
}

private long oldestJobAgeSecs(final JobStatus status) {
final var query = """
SELECT id, EXTRACT(EPOCH FROM (current_timestamp - created_at)) AS run_duration_seconds
FROM jobs WHERE status = ?::job_status
ORDER BY created_at ASC limit 1;
""";
final var result = ctx.fetchOne(query, status.getLiteral());
if (result == null) {
final var readableTimeField = "run_duration";
final var durationSecField = "run_duration_secs";
final var query = String.format("""
WITH
oldest_job AS (
SELECT id,
age(current_timestamp, created_at) AS %s
FROM jobs
WHERE status = '%s'
ORDER BY run_duration DESC
LIMIT 1)
SELECT id,
run_duration,
extract(epoch from run_duration) as %s
FROM oldest_job""", readableTimeField, status.getLiteral(), durationSecField);
final var res = ctx.fetch(query);
// unfortunately there are no good Jooq methods for retrieving a single record of a single column
// forcing the List cast.
final var duration = res.getValues(durationSecField, Double.class);

if (duration.size() == 0) {
return 0L;
}

// as double can have rounding errors, round down to remove noise.
return result.getValue("run_duration_seconds", Double.class).longValue();
return duration.get(0).longValue();
}

}

0 comments on commit 3f85fc4

Please sign in to comment.