0
0
DynamoDBquery~10 mins

Hierarchical data modeling in DynamoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Hierarchical data modeling
Start with Root Item
Add Child Items with Parent ID
Query by Parent ID to get Children
Repeat for deeper levels
Traverse hierarchy by querying each level
Hierarchical data modeling stores items with parent-child links, allowing queries to retrieve children by referencing parent IDs step-by-step.
Execution Sample
DynamoDB
Table: Employees
PK: EmployeeID
Attributes: Name, ManagerID

Insert:
{EmployeeID: 1, Name: 'Alice', ManagerID: null}
{EmployeeID: 2, Name: 'Bob', ManagerID: 1}
{EmployeeID: 3, Name: 'Carol', ManagerID: 1}

Query: Get employees where ManagerID = 1
This code models employees with managers as parents and queries employees reporting to Alice.
Execution Table
StepActionQuery ConditionResult Items
1Insert root employee AliceN/A{EmployeeID:1, Name:'Alice', ManagerID:null}
2Insert child employee BobN/A{EmployeeID:2, Name:'Bob', ManagerID:1}
3Insert child employee CarolN/A{EmployeeID:3, Name:'Carol', ManagerID:1}
4Query employees with ManagerID=1ManagerID=1[{EmployeeID:2, Name:'Bob'}, {EmployeeID:3, Name:'Carol'}]
5Query employees with ManagerID=2ManagerID=2[]
6End of queriesNo more childrenNo results
💡 No more child employees found for queried ManagerID, traversal ends.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
Employees TableEmptyAlice, Bob insertedAlice, Bob, Carol insertedAlice, Bob, CarolAlice, Bob, CarolAlice, Bob, Carol
Query ResultNoneNoneNone[Bob, Carol][][]
Key Moments - 2 Insights
Why does querying ManagerID=2 return no results?
Because no employees have ManagerID=2 in the table as shown in execution_table row 5, so the query returns an empty list.
How do we find all employees under Alice?
By querying employees where ManagerID=1 (Alice's EmployeeID), as shown in execution_table row 4, which returns Bob and Carol.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what items are returned when querying ManagerID=1?
A[{EmployeeID:1, Name:'Alice'}]
B[{EmployeeID:2, Name:'Bob'}, {EmployeeID:3, Name:'Carol'}]
C[]
D[{EmployeeID:3, Name:'Carol'}]
💡 Hint
Check execution_table row 4 under 'Result Items' for ManagerID=1 query.
At which step does the table contain the employee Carol?
AAfter Step 3
BAfter Step 2
CAfter Step 1
DAfter Step 4
💡 Hint
Look at execution_table rows 2 and 3 for when Bob and Carol are inserted.
If we add an employee Dave with ManagerID=2, what will querying ManagerID=2 return?
A[]
B[{EmployeeID:2, Name:'Bob'}]
C[{EmployeeID:4, Name:'Dave'}]
D[{EmployeeID:3, Name:'Carol'}]
💡 Hint
Adding Dave with ManagerID=2 means querying ManagerID=2 returns Dave, unlike current empty result in execution_table row 5.
Concept Snapshot
Hierarchical data modeling in DynamoDB:
- Store items with a unique ID and a ParentID (e.g., ManagerID)
- Root items have null ParentID
- Query children by filtering on ParentID
- Repeat queries to traverse deeper levels
- Enables tree-like data representation in flat tables
Full Transcript
Hierarchical data modeling in DynamoDB means storing data items with links to their parent items using a field like ManagerID. The root item has no parent (null). When we want to find children of a parent, we query items where the ParentID equals the parent's ID. For example, Alice has EmployeeID 1 and no manager. Bob and Carol have ManagerID 1, so they are Alice's children. Querying for ManagerID=1 returns Bob and Carol. If we query for ManagerID=2, no children exist yet, so the result is empty. This way, we can build and traverse a hierarchy step-by-step by querying each level's children.