Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: Allow covering relation queries with minimal index (#6581)
* Apply linter changes on files I'm about to update My actual changes were quite difficult to find when buried in this sea of style changes, which were getting automatically applied during a pre-commit hook. Here I just run the hooks against the files I'm going to be touching in the following commit, so that a reviewer can ignore these automatically generated diffs and just view the meaningful commit. * perf: Allow covering relation queries with minimal index When finding objects through a relation, we're sending Mongo queries that look like this: ``` db.getCollection('_Join:foo:bar').find({ relatedId: { $in: [...] } }); ``` From the result of that query, we're only reading the `owningId` field, so we can start by adding it as a projection: ``` db.getCollection('_Join:foo:bar') .find({ relatedId: { $in: [...] } }) .project({ owningId: 1 }); ``` 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: ``` db.getCollection('_Join:roles:_Role').createIndex( { relatedId: 1, owningId: 1 }, { background: true } ); ``` 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 the `owningId` 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 }`.
- Loading branch information