diff --git a/pkg/jobs/utils.go b/pkg/jobs/utils.go index db59226ccf90..1e5f78a6cb33 100644 --- a/pkg/jobs/utils.go +++ b/pkg/jobs/utils.go @@ -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,