Skip to content

perf(mysql): force per-query-shape indexes for relationship queries#3173

Open
josephschorr wants to merge 1 commit into
authzed:mainfrom
josephschorr:mysql-rel-query-perf
Open

perf(mysql): force per-query-shape indexes for relationship queries#3173
josephschorr wants to merge 1 commit into
authzed:mainfrom
josephschorr:mysql-rel-query-perf

Conversation

@josephschorr

Copy link
Copy Markdown
Member

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_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.

Mapping

Index Sort order Query shapes
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 (emitting MySQL FORCE INDEX (...)) for these shapes and no hint otherwise. QueryRelationships and ReverseQueryRelationships apply the hint based on the request's query shape. uq_relation_tuple_namespace is now documented as a uniqueness-only constraint — it is not used to serve any read shape.

Testing

  • New unit test TestIndexingHintForQueryShape covering the shape → index/sort mapping.
  • New BenchmarkMySQLRelQueries benchmark exercising the affected forward and reverse query shapes (requires Docker).
  • mage testds:mysql passes locally.

@josephschorr josephschorr requested a review from a team as a code owner June 11, 2026 16:45
@github-actions github-actions Bot added area/datastore Affects the storage system area/tooling Affects the dev or user toolchain (e.g. tests, ci, build tools) labels Jun 11, 2026
@josephschorr josephschorr force-pushed the mysql-rel-query-perf branch from 5428342 to 58d3946 Compare June 11, 2026 16:46
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.
@josephschorr josephschorr force-pushed the mysql-rel-query-perf branch from 58d3946 to 126eccd Compare June 11, 2026 16:54
@codecov

codecov Bot commented Jun 11, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/datastore Affects the storage system area/tooling Affects the dev or user toolchain (e.g. tests, ci, build tools)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant