Laravel database queue indexes #40290
Unanswered
ThomasJamesCrawford
asked this question in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
👋
We are using a database queue (postgres) for jobs with a long delay to free up redis memory.
This is the query it generates to pull available jobs.
select * from "jobs" where "queue" = ? and (("reserved_at" is null and "available_at" <= ?) or ("reserved_at" <= ?)) order by "id" asc limit 1 FOR UPDATE SKIP LOCKED;
The problem we have is we run into queries like this. The query planner seems to choose the pkey index because it expects there to be many more rows returned than there will be and it's estimating the cost to sort the results is higher than using the already sorted index.
This combination of order by "id" and limit 1 makes it really hard to get around this.
We have dropped the order by "id" asc from the query to get it consistently to use our indexes.
Potential ways to fix it could be adding more fields to the order by
select * from "jobs" where "queue" = ? and (("reserved_at" is null and "available_at" <= ?) or ("reserved_at" <= ?)) order by "available_at, "id" asc, "reserved_at" limit 1 FOR UPDATE SKIP LOCKED;
or perhaps there could be an easy way to remove the order by from the query?
Beta Was this translation helpful? Give feedback.
All reactions