Further optimize queries for better index usage

This commit is contained in:
2026-03-03 15:54:51 +01:00
parent 763e0c8d7c
commit 6da41982ce
2 changed files with 7 additions and 10 deletions

View File

@@ -718,7 +718,7 @@ func (r *JobRepository) StopJobsExceedingWalltimeBy(seconds int) error {
Set("job_state", schema.JobStateFailed).
Where("job.job_state = 'running'").
Where("job.walltime > 0").
Where("(? - job.start_time) > (job.walltime + ?)", currentTime, seconds).
Where("job.start_time < (? - job.walltime)", currentTime-int64(seconds)).
RunWith(r.DB).Exec()
if err != nil {
cclog.Warnf("Error while stopping jobs exceeding walltime: %v", err)

View File

@@ -270,24 +270,21 @@ func (r *JobRepository) FindConcurrentJobs(
stopTime = startTime + int64(job.Duration)
}
// Time buffer constants for finding overlapping jobs
// overlapBufferStart: 10s grace period at job start to catch jobs starting just after
// Time buffer constant for finding overlapping jobs
// overlapBufferEnd: 200s buffer at job end to account for scheduling/cleanup overlap
const overlapBufferStart = 10
const overlapBufferEnd = 200
startTimeTail := startTime + overlapBufferStart
stopTimeTail := stopTime - overlapBufferEnd
startTimeFront := startTime + overlapBufferEnd
// Reminder: BETWEEN Queries are slower and dont use indices as frequently: Can this be optimized?
queryRunning := query.Where("job.job_state = ?").Where("(job.start_time BETWEEN ? AND ? OR job.start_time < ?)",
"running", startTimeTail, stopTimeTail, startTime)
queryRunning := query.Where("job.job_state = ?", "running").
Where("job.start_time <= ?", stopTimeTail)
// Get At Least One Exact Hostname Match from JSON Resources Array in Database
queryRunning = queryRunning.Where("EXISTS (SELECT 1 FROM json_each(job.resources) WHERE json_extract(value, '$.hostname') = ?)", hostname)
query = query.Where("job.job_state != ?").Where("((job.start_time BETWEEN ? AND ?) OR (job.start_time + job.duration) BETWEEN ? AND ? OR (job.start_time < ?) AND (job.start_time + job.duration) > ?)",
"running", startTimeTail, stopTimeTail, startTimeFront, stopTimeTail, startTime, stopTime)
query = query.Where("job.job_state != ?", "running").
Where("job.start_time < ?", stopTimeTail).
Where("(job.start_time + job.duration) > ?", startTimeFront)
// Get At Least One Exact Hostname Match from JSON Resources Array in Database
query = query.Where("EXISTS (SELECT 1 FROM json_each(job.resources) WHERE json_extract(value, '$.hostname') = ?)", hostname)