One-to-many relationship patterns in DynamoDB - Time & Space Complexity
When working with one-to-many relationships in DynamoDB, it is important to understand how the time to get data grows as the number of related items increases.
We want to know how the cost changes when we fetch many related records for one main item.
Analyze the time complexity of the following DynamoDB query pattern.
// Query to get all orders for a single customer
const params = {
TableName: "Orders",
KeyConditionExpression: "customerId = :cid",
ExpressionAttributeValues: {
":cid": { S: "123" }
}
};
const result = await dynamodb.query(params).promise();
This code fetches all orders linked to one customer using a query on the partition key.
Look for repeated actions that affect performance.
- Primary operation: Reading each order item for the customer.
- How many times: Once per order item returned by the query.
As the number of orders for a customer grows, the query reads more items.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 item reads |
| 100 | About 100 item reads |
| 1000 | About 1000 item reads |
Pattern observation: The number of operations grows directly with the number of related items.
Time Complexity: O(n)
This means the time to get all related items grows linearly with how many items there are.
[X] Wrong: "Querying related items always takes constant time regardless of how many items exist."
[OK] Correct: Each related item must be read, so more items mean more work and longer time.
Understanding how queries scale with related data helps you design better data models and answer questions confidently in interviews.
"What if we added pagination to limit the number of related items returned at once? How would the time complexity change?"
Practice
PartitionKey in a one-to-many relationship pattern in DynamoDB?Solution
Step 1: Understand PartitionKey role
The PartitionKey groups related items together in DynamoDB, acting like a folder for related data.Step 2: Differentiate PartitionKey from SortKey
The SortKey uniquely identifies each item within the group, but PartitionKey groups them all.Final Answer:
To group all related items under one main item -> Option AQuick Check:
PartitionKey groups related items = D [OK]
- Confusing PartitionKey with SortKey
- Thinking PartitionKey uniquely identifies each item
- Assuming PartitionKey stores data type
Solution
Step 1: Understand SortKey purpose
The SortKey uniquely identifies each related item within the same PartitionKey group.Step 2: Eliminate incorrect options
SortKey cannot be the same for all items or equal to PartitionKey or table name.Final Answer:
SortKey as a unique identifier for each related item -> Option CQuick Check:
SortKey uniquely identifies items = C [OK]
- Using same SortKey for all items
- Confusing SortKey with PartitionKey
- Assigning table name as SortKey
Query(CustomerID='123') return?Solution
Step 1: Understand Query with PartitionKey only
Querying with PartitionKey='123' returns all items sharing that PartitionKey, i.e., all orders for customer 123.Step 2: Clarify SortKey role in query
SortKey is not specified, so all SortKey values under PartitionKey='123' are returned.Final Answer:
All orders for customer with ID 123 -> Option BQuick Check:
Query by PartitionKey returns all related items = A [OK]
- Thinking query returns single item without SortKey
- Confusing PartitionKey and SortKey in query
- Expecting error when SortKey is not specified
Solution
Step 1: Understand uniqueness of PartitionKey + SortKey
In DynamoDB, the combination of PartitionKey and SortKey must be unique for each item.Step 2: Analyze inserting duplicate keys
Inserting an item with the same PartitionKey and SortKey overwrites the existing item.Final Answer:
The second insert overwrites the first comment -> Option AQuick Check:
Duplicate keys overwrite existing item = A [OK]
- Expecting error on duplicate keys
- Thinking both items are stored separately
- Confusing PartitionKey uniqueness with SortKey
Solution
Step 1: Understand the hierarchy
Customer has many orders, and each order has many items, so two levels of one-to-many.Step 2: Design composite keys for nested relationships
Using PartitionKey as CustomerID#OrderID groups items by order under each customer; SortKey as ItemID identifies each item.Step 3: Eliminate other options
Other options mix keys incorrectly, losing the hierarchical grouping.Final Answer:
Use PartitionKey as CustomerID#OrderID and SortKey as ItemID -> Option DQuick Check:
Composite PartitionKey groups nested one-to-many = B [OK]
- Using wrong key order losing hierarchy
- Not combining keys for nested relationships
- Confusing PartitionKey and SortKey roles
Ready to prove it?
Pass the test and this pattern moves unknown
