Skip to content

Commit

Permalink
sql: improve query plan for find-running-job-of-type query
Browse files Browse the repository at this point in the history
This commit improves the query plan for the `find-running-jobs-of-type`
query by removing an `ORDER BY` clause if a job ID to ignore was not
given. This can eliminate an index join from the query plan in some
cases, making the query play more efficient.

Informs #107405

Release note: None
  • Loading branch information
mgartner committed Jul 25, 2023
1 parent 4618dbf commit 488d74a
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions pkg/jobs/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,21 @@ func RunningJobExists(
typeStrs = s.String()
}

orderBy := " ORDER BY created"
if ignoreJobID == jobspb.InvalidJobID {
// There is no need to order by the created column if there is no job to
// ignore.
orderBy = ""
}

stmt := `
SELECT
id
FROM
system.jobs
WHERE
job_type IN ` + typeStrs + ` AND
status IN ` + NonTerminalStatusTupleString + `
ORDER BY created
status IN ` + NonTerminalStatusTupleString + orderBy + `
LIMIT 1`
it, err := txn.QueryIterator(
ctx,
Expand Down

0 comments on commit 488d74a

Please sign in to comment.