Many-to-Many Joins and Bridge Tables/Index bridge-table foreign keys

Index both sides of the relationship for join speed.

Section: Design notes

Index bridge-table foreign keys

sql
sql
CREATE INDEX idx_user_roles_user_id ON user_roles(user_id);
CREATE INDEX idx_user_roles_role_id ON user_roles(role_id);
Explanation

Bridge tables are heavily joined and benefit from focused indexes.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing Design notes
Join through a bridge table
Resolve many-to-many relationships with two joins.
OpenIn sheetsql1 tag match
Aggregate across a many-to-many relationship
Count related entities after joining through the bridge.
Find posts with all required tags
Join and group, then compare distinct matched tags to the required count.