Why single-table design matters in DynamoDB - Performance Analysis
When using DynamoDB, how we organize data affects how fast queries run.
We want to see how single-table design changes the work DynamoDB does as data grows.
Analyze the time complexity of querying data in a single-table design.
{
TableName: "MyTable",
KeyConditionExpression: "PK = :pkValue",
ExpressionAttributeValues: {
":pkValue": "USER#123"
}
}
This query fetches all items with a partition key for one user in a single table.
Look at what repeats when DynamoDB runs this query.
- Primary operation: Reading items with the same partition key.
- How many times: Once per item matching the key, sequentially.
As the number of items for a user grows, the work grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 reads |
| 100 | 100 reads |
| 1000 | 1000 reads |
Pattern observation: The work grows directly with the number of items for that key.
Time Complexity: O(n)
This means the time to get all items grows in a straight line with how many items match the key.
[X] Wrong: "Using a single table means queries are always faster no matter what."
[OK] Correct: Even with one table, reading many items for a key takes more time because each item must be read.
Understanding how data layout affects query speed shows you think about real database work and efficiency.
What if we added a sort key to narrow queries? How would that change the time complexity?
Practice
Solution
Step 1: Understand single-table design purpose
Single-table design groups related data in one table to reduce complexity and speed up queries.Step 2: Compare options with this purpose
Only It stores related data together for faster access. correctly states it stores related data together for faster access.Final Answer:
It stores related data together for faster access. -> Option BQuick Check:
Single-table design = faster access [OK]
- Thinking it increases database calls
- Believing it needs more tables
- Ignoring key design importance
Solution
Step 1: Recall partition key definition
The partition key must be a single attribute that uniquely partitions data.Step 2: Analyze options
PartitionKey: userIduses a single attribute userId, which is valid.PartitionKey: {userId, orderId}tries to use multiple attributes as one key, which is invalid syntax.PartitionKey: userId + orderIdconcatenates keys but is not a direct attribute definition.PartitionKey: orderIduses orderId alone, which may not be unique.Final Answer:
<code>PartitionKey: userId</code> -> Option AQuick Check:
Partition key = single attribute [OK]
- Using multiple attributes directly as partition key
- Concatenating keys without defining attribute
- Choosing non-unique keys
UserId and sort key OrderId, what will this query return?SELECT * FROM Orders WHERE UserId = '123' AND OrderId = 'A1'
Solution
Step 1: Understand key conditions in query
The query filters by partition key UserId = '123' and sort key OrderId = 'A1', so it targets one specific item.Step 2: Determine result based on keys
Since both keys are specified, only the single matching order for user 123 with OrderId 'A1' will be returned.Final Answer:
Only the order with OrderId 'A1' for user 123 -> Option CQuick Check:
Partition + sort key = single item [OK]
- Assuming query returns all orders for user
- Ignoring sort key in query
- Thinking query syntax is invalid
UserId and sort key OrderId. Your query to get all orders for a user returns no results, but you know data exists. What is the likely mistake?Solution
Step 1: Recall query requirements for partition and sort keys
In DynamoDB, queries must specify the partition key. Querying only by sort key is invalid and returns no results.Step 2: Analyze options
You queried only by sort key without partition key. matches the mistake of querying only by sort key. You used the wrong table name in the query. and D are possible but less likely if data exists. You queried only by partition key without sort key. is valid and returns all items with that partition key.Final Answer:
You queried only by sort key without partition key. -> Option AQuick Check:
Query must include partition key [OK]
- Querying only by sort key
- Using wrong table name
- Assuming table not created
Solution
Step 1: Understand single-table design key patterns
Using a partition key as UserId groups all user data together. Adding a sort key with ItemType#OrderId lets you separate user profile and orders.Step 2: Evaluate options for efficient queries
Partition key: UserId, Sort key: ItemType#OrderId allows querying all orders by filtering sort key starting with 'Order' and fetching profile with sort key 'Profile'. Other options mix keys making queries inefficient or impossible.Final Answer:
Partition key: UserId, Sort key: ItemType#OrderId -> Option DQuick Check:
Partition by user, sort by item type [OK]
- Using orderId as partition key
- Not distinguishing item types in sort key
- Combining keys incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
