Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve query performance #17862

Merged
merged 4 commits into from
Oct 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = 'failed' OR jobs.status = 'succeeded' OR jobs.status = 'cancelled');
AND jobs.status IN ('failed', 'succeeded', 'cancelled');
""";
final var queryResults = ctx.fetch(query);
final var statuses = queryResults.getValues("status", JobStatus.class);
Expand All @@ -159,32 +159,17 @@ SELECT status, extract(epoch from age(updated_at, created_at)) AS sec FROM jobs
}

private long oldestJobAgeSecs(final JobStatus status) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT/ FAR (For another review): This is not covert by a UTest. Should we write a story to cover those queries on a container DB. That would make those changes much safer.

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) {
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) {
return 0L;
}

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

}