0
0
DynamoDBquery~10 mins

Many-to-many with GSI overloading in DynamoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Many-to-many with GSI overloading
Write item with PK, SK, and GSI keys
Store item in main table
GSI indexes item by GSI PK and GSI SK
Query main table by PK and SK for one side
Query GSI by GSI PK and GSI SK for other side
Combine results to get many-to-many relationships
Items are stored with primary keys and overloaded GSI keys to represent many-to-many links. Queries use main keys or GSI keys to find related items from either side.
Execution Sample
DynamoDB
PUT item: {PK: 'USER#1', SK: 'GROUP#A', GSI1PK: 'GROUP#A', GSI1SK: 'USER#1'}
PUT item: {PK: 'USER#2', SK: 'GROUP#A', GSI1PK: 'GROUP#A', GSI1SK: 'USER#2'}
QUERY PK='USER#1' to find groups
QUERY GSI1PK='GROUP#A' to find users
Store user-group links with PK as user and SK as group, and GSI1PK as group and GSI1SK as user to enable querying from both sides.
Execution Table
StepActionInputIndex UsedResult
1Put item{PK:'USER#1', SK:'GROUP#A', GSI1PK:'GROUP#A', GSI1SK:'USER#1'}Main TableItem stored
2Put item{PK:'USER#2', SK:'GROUP#A', GSI1PK:'GROUP#A', GSI1SK:'USER#2'}Main TableItem stored
3QueryPK='USER#1'Main TableReturns item with SK='GROUP#A'
4QueryGSI1PK='GROUP#A'GSI1Returns items with GSI1SK='USER#1' and 'USER#2'
5CombineResults from steps 3 and 4N/AUser 1 belongs to Group A; Group A has Users 1 and 2
6ExitNo more queriesN/AExecution ends
💡 All relevant items stored and queried; many-to-many relationships retrieved
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
Main Table Itemsempty[{PK:'USER#1', SK:'GROUP#A', GSI1PK:'GROUP#A', GSI1SK:'USER#1'}][{PK:'USER#1', SK:'GROUP#A', ...}, {PK:'USER#2', SK:'GROUP#A', ...}][{PK:'USER#1', SK:'GROUP#A', ...}, {PK:'USER#2', SK:'GROUP#A', ...}][{PK:'USER#1', SK:'GROUP#A', ...}, {PK:'USER#2', SK:'GROUP#A', ...}][{PK:'USER#1', SK:'GROUP#A', ...}, {PK:'USER#2', SK:'GROUP#A', ...}]
Key Moments - 3 Insights
Why do we store the same relationship twice with different keys?
Because DynamoDB queries efficiently only on primary keys or GSIs, storing the relationship with PK=User and GSI1PK=Group lets us query from either side (user to groups or group to users). See steps 1, 2, 3, and 4 in the execution_table.
How does querying the GSI help find all users in a group?
The GSI is set with GSI1PK as the group ID and GSI1SK as the user ID, so querying GSI1PK='GROUP#A' returns all users linked to that group. This is shown in step 4.
What happens if we query the main table with a group ID as PK?
It returns no results because the main table's PK is the user ID, not the group ID. To find users by group, we must query the GSI. This is why GSI overloading is needed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is returned when querying PK='USER#1' at step 3?
AItems with SK='GROUP#A'
BItems with SK='USER#1'
CItems with GSI1PK='GROUP#A'
DNo items
💡 Hint
Check step 3 in the execution_table under Result column
At which step do we query the GSI to find all users in a group?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the Index Used column in the execution_table
If we remove GSI1PK and GSI1SK from items, what changes in the execution?
AWe can still query users by group using the main table
BWe can query groups by user using GSI
CWe cannot query users by group efficiently
DNo change in querying
💡 Hint
Refer to key_moments about why GSI overloading is needed
Concept Snapshot
Many-to-many with GSI overloading in DynamoDB:
- Store each link twice: PK=User, SK=Group and GSI1PK=Group, GSI1SK=User
- Query main table by PK to find groups for a user
- Query GSI by GSI1PK to find users in a group
- Enables efficient queries from both sides
- GSI overloading means using GSIs to index relationships differently
Full Transcript
This visual execution shows how to model many-to-many relationships in DynamoDB using GSI overloading. Each relationship is stored as an item with primary keys (PK and SK) representing one side (user to group) and GSI keys (GSI1PK and GSI1SK) representing the other side (group to user). We put two items linking users and groups. Then, querying the main table by PK='USER#1' returns groups for that user. Querying the GSI by GSI1PK='GROUP#A' returns all users in that group. This approach allows efficient queries from either side of the many-to-many relationship. The execution table traces each step, showing how items are stored and queried. The variable tracker shows how the main table items grow after each put. Key moments clarify why storing the relationship twice is necessary and how the GSI enables querying by group. The visual quiz tests understanding of querying steps and the role of GSI keys. The concept snapshot summarizes the pattern for quick reference.