perf(mysql): force per-query-shape indexes for relationship queries#3173
Open
josephschorr wants to merge 1 commit into
Open
perf(mysql): force per-query-shape indexes for relationship queries#3173josephschorr wants to merge 1 commit into
josephschorr wants to merge 1 commit into
Conversation
5428342 to
58d3946
Compare
MySQL's optimizer would sometimes choose a suboptimal index for relationship
reads, and the prior index-to-query-shape mapping was inaccurate -- several
shapes pointed at indexes that don't serve them well, with TODOs noting as
much. Force the index that actually matches each query shape, and use the sort
order that index provides.
relation_tuple is clustered on the synthetic `id` primary key, so the
resource-keyed UNIQUE index (uq_relation_tuple_living) plays the role that the
primary key plays in CRDB/Postgres for forward query shapes. The mapping is:
- uq_relation_tuple_living (ByResource):
CheckPermissionSelectDirectSubjects, CheckPermissionSelectIndirectSubjects,
AllSubjectsForResources, FindResourceOfType
- ix_relation_tuple_by_subject (BySubject):
MatchingResourcesForSubject
- ix_relation_tuple_by_subject_relation (BySubject):
FindResourceAndSubjectWithRelations, FindSubjectOfTypeAndRelation,
FindResourceRelationForSubjectRelation
IndexingHintForQueryShape returns a forcedIndex (which emits MySQL
`FORCE INDEX (...)`) for these shapes and no hint otherwise. QueryRelationships
and ReverseQueryRelationships apply the hint based on the request's query shape.
The uq_relation_tuple_namespace UNIQUE index is now documented as a
uniqueness-only constraint; it is not used to serve any read shape.
Adds a TestIndexingHintForQueryShape unit test for the shape -> index/sort
mapping and a BenchmarkMySQLRelQueries benchmark covering the affected forward
and reverse query shapes.
58d3946 to
126eccd
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
MySQL's query optimizer would sometimes choose a suboptimal index for relationship reads, and the prior index-to-query-shape mapping was inaccurate — several shapes pointed at indexes that don't serve them well (with
TODOs in the code noting exactly that). This PR forces the index that actually matches each query shape and uses the sort order that index provides, bringing MySQL in line with the explicit index-forcing already done for CRDB, Postgres, and Spanner.relation_tupleis clustered on the syntheticidprimary key, so the resource-keyedUNIQUEindex (uq_relation_tuple_living) plays the role that the primary key plays in CRDB/Postgres for forward query shapes.Mapping
uq_relation_tuple_livingByResourceCheckPermissionSelectDirectSubjects,CheckPermissionSelectIndirectSubjects,AllSubjectsForResources,FindResourceOfTypeix_relation_tuple_by_subjectBySubjectMatchingResourcesForSubjectix_relation_tuple_by_subject_relationBySubjectFindResourceAndSubjectWithRelations,FindSubjectOfTypeAndRelation,FindResourceRelationForSubjectRelationIndexingHintForQueryShapereturns aforcedIndex(emitting MySQLFORCE INDEX (...)) for these shapes and no hint otherwise.QueryRelationshipsandReverseQueryRelationshipsapply the hint based on the request's query shape.uq_relation_tuple_namespaceis now documented as a uniqueness-only constraint — it is not used to serve any read shape.Testing
TestIndexingHintForQueryShapecovering the shape → index/sort mapping.BenchmarkMySQLRelQueriesbenchmark exercising the affected forward and reverse query shapes (requires Docker).mage testds:mysqlpasses locally.