I have a table like this:
CREATE TABLE IF NOT EXISTS `jobs` (
`job_id` varchar(36) NOT NULL,
`job_status` varchar(30) NOT NULL,
`created_at` datetime NOT NULL,
`lease_date` datetime,
`priority` int NOT NULL,
PRIMARY KEY(`job_id`),
INDEX `job_status_priority_lease_date` (`job_status`, `priority`, `lease_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
The full query I want to perform on the table would be the following:
select * from jobs
where job_status="IN_PROGRESS"
and lease_date<"10minutesago"
order by
priority
limit 100
I am implementing database as a job queue. 10 min is like the job time out, I only want to act on the jobs that are already timed out. Jobs has different priority so I was ordering by it. Also, I want to make my DB job queue behave like FIFO, that's why my index includes lease_date
.
I definitely want my results to be ordered by Priority, e.g. two chunks of the results with Priority 0 and 1. I don't need my results to be strictly ordered by lease_date
, as long as they are Ascending order in their own chunks that is good enough for me. e.g. result set:
priority lease_date
0 2017-08-19
0 2018-09-20
1 2016-08-20
1 2018-10-20
Is my index enough for that? How much can I optimize the query and build an index to boost the performance as much as possible?