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 adding a hint to use the `jobs_status_created_idx` and 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 cockroachdb#107405

Release note: None
  • Loading branch information
mgartner committed Oct 9, 2023
1 parent be961bf commit 4945cf0
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions pkg/jobs/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,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
system.jobs@jobs_status_created_idx
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 4945cf0

Please sign in to comment.