Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an index to better support smaller_number_is_higher_priority #1213

Merged

Conversation

mkrfowler
Copy link
Contributor

The preexisting index (introduced in #726) gave a direct answer to the materialized subquery used to find candidate jobs, subject to the ordering rules that were in place at that time. In #883, GoodJob deprecated that ordering in favour of a lower-is-more-important scheme, aligning with Active Job, but the index was not updated at that time.

I observed a substantial improvement in job throughput on applying an equivalent index, in the context of a jobs table with some 10M+ completed jobs. This catches the index change mentioned in #1005.

Plan changes for the subquery (admittedly, not the most recent GoodJob version) before and after applying:
Before:
Limit  (cost=14528.90..14529.15 rows=100 width=44) (actual time=517.784..517.794 rows=70 loops=1)
  ->  Sort  (cost=14528.90..14532.43 rows=1415 width=44) (actual time=517.783..517.787 rows=70 loops=1)
        Sort Key: priority, created_at
        Sort Method: quicksort  Memory: 30kB
        ->  Bitmap Heap Scan on good_jobs  (cost=8951.83..14474.81 rows=1415 width=44) (actual time=111.465..517.719 rows=70 loops=1)
              Recheck Cond: (finished_at IS NULL)
              Rows Removed by Index Recheck: 1168042
              Filter: ((scheduled_at <= '2024-01-12 02:51:38.927646'::timestamp without time zone) OR (scheduled_at IS NULL))
              Rows Removed by Filter: 847
              Heap Blocks: exact=557 lossy=99411
              ->  Bitmap Index Scan on index_good_jobs_on_concurrency_key_when_unfinished  (cost=0.00..8951.48 rows=1415 width=0) (actual time=56.387..56.388 rows=552240 loops=1)
Planning Time: 1.276 ms
Execution Time: 518.545 ms

After (a different index name):

Limit  (cost=5.90..5.91 rows=1 width=44) (actual time=0.022..0.023 rows=0 loops=1)
  ->  Sort  (cost=5.90..5.91 rows=1 width=44) (actual time=0.021..0.022 rows=0 loops=1)
        Sort Key: priority, created_at
        Sort Method: quicksort  Memory: 25kB
        ->  Index Scan using index_good_jobs_jobs_on_priority_created_at_when_unfinished on good_jobs  (cost=0.12..5.89 rows=1 width=44) (actual time=0.004..0.004 rows=0 loops=1)
              Filter: ((scheduled_at <= '2024-01-12 02:51:38.927646'::timestamp without time zone) OR (scheduled_at IS NULL))
Planning Time: 0.668 ms
Execution Time: 0.041 ms

The preexisting index (introduced in bensheldon#726) gave a direct answer to the
materialized subquery used to find candidate jobs, subject to the ordering rules
that were in place at that time.  In bensheldon#883, `GoodJob` deprecated that ordering in
favour of a lower-is-more-important scheme, aligning with Active Job.

When there are a substantial number of completed jobs, the `priority desc` index
does not allow for a straight index scan, and may not actually be used by the
planner.  Aligning the sort orders here allows for the subquery to be satisfied
directly.

Although `ASC NULLS LAST` is the default (as observable in the generated schema)
it seems appropriate to be explicit in the actual index declaration, although
mostly for consistency with the previous version.
@mkrfowler mkrfowler force-pushed the ascending-priority-job-queue-index branch from c793636 to 38a4f49 Compare January 14, 2024 23:10
@bensheldon
Copy link
Owner

Thank you! This is excellent work and I'm impressed you navigated all of the various migration configuration! 🚀

@bensheldon bensheldon added the bug Something isn't working label Jan 19, 2024
@bensheldon bensheldon merged commit 6a7332a into bensheldon:main Jan 19, 2024
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Development

Successfully merging this pull request may close these issues.

2 participants