Skip to content

Commit

Permalink
Fix performance issues with delayed_jobs by extending index
Browse files Browse the repository at this point in the history
We found out that when having a lot of delayed_jobs rows in the table,
postgresql query planner did a sequential scan of the table.
This was due to the query of cc_workers to fetch new jobs being:

```
SELECT *
FROM "delayed_jobs"
WHERE (((("run_at" <= '2023-06-27 07:18:28.061781+0000')
AND ("locked_at" IS NULL))
(.....)
ORDER BY "priority" ASC, "run_at" ASC
LIMIT 1 FOR Update;
```

As an order by is used on `priority` the index over the columns
`queue, locked_at, locked_by, failed_at, run_at` is not used.

This is more severe as the query above gets more and more expensive
by increasing row count and is queried a lot. Every worker every few
seconds does this select. This quickly can become a load issue for the
database.

This change adds the `priority` column to the already existing
index `delayed_jobs_reserve`. Improves query times as well as
database load significantly in our tests.
  • Loading branch information
FloThinksPi committed Jun 27, 2023
1 parent cd55e11 commit e4c29fc
Showing 1 changed file with 11 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Sequel.migration do
up do
drop_index :delayed_jobs, [], name: :delayed_jobs_reserve
add_index :delayed_jobs, [:queue, :locked_at, :locked_by, :failed_at, :run_at, :priority], name: :delayed_jobs_reserve
end
down do
drop_index :delayed_jobs, [], name: :delayed_jobs_reserve
add_index :delayed_jobs, [:queue, :locked_at, :locked_by, :failed_at, :run_at], name: :delayed_jobs_reserve
end
end

0 comments on commit e4c29fc

Please sign in to comment.