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

⚡️ Sort get() by visible #13

Merged
merged 1 commit into from
Oct 23, 2024
Merged

⚡️ Sort get() by visible #13

merged 1 commit into from
Oct 23, 2024

Conversation

alecgibson
Copy link
Collaborator

At the moment, when calling get(), we:

  • query on deleted and visible
  • sort on _id

We have indexes for both of these things, but separately, which results in an inefficient query, since MongoDB will have to check both indexes and can result in a complete index scan, which isn't particularly great.

We sort by _id presumably to get the oldest job (since _id is an ObjectId whose sort order correlates to creation time).

However, we probably actually want the job that was visible first.

This change updates to sort to use visible, which also means that the query and sort can use the same index.

At the moment, when calling `get()`, we:

 - query on `deleted` and `visible`
 - sort on `_id`

We have indexes for both of these things, but separately, which results
in an inefficient query, since MongoDB will have to check both indexes
and can result in a complete index scan, which isn't particularly great.

We sort by `_id` presumably to get the oldest job (since `_id` is an
`ObjectId` whose sort order correlates to creation time).

However, we probably actually want the job that was visible first.

This change updates to sort to use `visible`, which also means that the
query and sort can use the same index.
@@ -130,7 +130,7 @@ export class MongoDBQueue<T = any> {
visible: {$lte: now()},
};
const sort: Sort = {
_id: 1,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we would have to change our index for it to work best

this.col.createIndex({deleted: 1, visible: 1}),

to

this.col.createIndex({visible: 1, deleted: 1}),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure it it belongs to this PR though

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely a different change, which will need an index regeneration.

I'm also not sure it's needed, since I think in theory MongoDB can still leverage the index for a non-prefix sort since deleted: {$exists: false} counts as an equality condition.

@alecgibson alecgibson merged commit 8d04c21 into main Oct 23, 2024
2 checks passed
@alecgibson alecgibson deleted the sort-visible branch October 23, 2024 14:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants