perf: Allow covering relation queries with minimal index #6581
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When finding objects through a relation, we're sending Mongo queries that look like this:
From the result of that query, we're only reading the
owningId
field, so we can start by adding it as a projection:This seems like the perfect example of a query that could be satisfied with an index scan: we are querying on one field, and only need one field from the matching document.
For example, this can allow users to speed up the fetching of user roles in authentication, because they query a
roles
relation on the_Role
collection. To add a covering index on that, you could now add an index like the following:One caveat there is that the index I propose above doesn't include the
_id
column. For the query in question, we don't actually care about the ID of the row in the join table, just theowningId
field, so we can avoid some overhead of putting the_id
column into the index if we can also drop it from the projection. This requires adding a small special case to the MongoStorageAdapter, because the_id
field is special: you have to opt-out of using it by projecting{ _id: 0 }
.It appears that there is also now an automatic commit hook that applies some linting and styling rules, so I also broke this PR up into two commits: one that just applies those automatic fixes, and one that then makes the actual change. Hopefully this makes. it easier to review the diff!