Bird
Raised Fist0
DynamoDBquery~10 mins

Single-table design methodology in DynamoDB - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Single-table design methodology
Identify Entities
↓
Define Primary Key
↓
Design Sort Key Patterns
↓
Use Attributes for Item Types
↓
Query by Partition and Sort Key
↓
Fetch Related Items Together
↓
Optimize Access Patterns
↓
Single Table Holds All Data
This flow shows how to design a single DynamoDB table to hold multiple entity types by carefully choosing keys and attributes to support all queries.
Execution Sample
DynamoDB
PK = 'USER#123'
SK = 'PROFILE#123'
ItemType = 'UserProfile'
Attributes = {"Name": "Alice", "Age": 30}
This example shows storing a user profile item with partition key and sort key encoding the user ID and item type.
Execution Table
StepActionPK ValueSK ValueItemTypeAttributesResult
1Create user profile itemUSER#123PROFILE#123UserProfile{"Name": "Alice", "Age": 30}Item stored
2Create user order itemUSER#123ORDER#456Order{"OrderDate": "2024-06-01", "Total": 50}Item stored
3Query all items for USER#123USER#123Starts with ''N/AN/AReturns profile and order items
4Query only orders for USER#123USER#123Starts with 'ORDER#'N/AN/AReturns only order items
5Add product itemPRODUCT#789METADATA#789Product{"Name": "Book", "Price": 15}Item stored
6Query product by PKPRODUCT#789METADATA#789N/AN/AReturns product item
7ExitN/AN/AN/AN/AAll items stored and queryable in one table
💡 All entities stored in one table with composite keys supporting queries.
Variable Tracker
VariableStartAfter 1After 2After 5Final
PKnullUSER#123USER#123PRODUCT#789PRODUCT#789
SKnullPROFILE#123ORDER#456METADATA#789METADATA#789
ItemTypenullUserProfileOrderProductProduct
Attributes{}{"Name": "Alice", "Age": 30}{"OrderDate": "2024-06-01", "Total": 50}{"Name": "Book", "Price": 15}{"Name": "Book", "Price": 15}
Key Moments - 2 Insights
Why do we use composite keys (PK and SK) instead of separate tables?
Using composite keys lets us store different entity types in one table and query related items efficiently, as shown in execution_table rows 3 and 4 where queries return multiple item types.
How does the sort key help in filtering items?
The sort key encodes item type or other info, so queries can filter by SK prefix (rows 3 and 4), returning only desired items without scanning the whole table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what items are returned when querying PK='USER#123'?
ABoth user profile and order items
BOnly the order item
COnly the user profile item
DNo items
💡 Hint
Check the 'Result' column in row 3 of execution_table.
At which step is a product item added to the table?
AStep 2
BStep 4
CStep 5
DStep 6
💡 Hint
Look at the 'Action' and 'ItemType' columns in execution_table rows.
If we changed the sort key of the order item to 'PROFILE#456', what would happen when querying orders by SK prefix 'ORDER#'?
AThe order item would still be returned
BThe order item would not be returned
CAll items would be returned
DQuery would fail
💡 Hint
Refer to how SK prefix filtering works in execution_table row 4.
Concept Snapshot
Single-table design in DynamoDB:
- Use one table for all entities
- PK identifies partition (e.g., user)
- SK encodes item type and sort order
- Attributes store item data
- Query by PK and SK prefixes to fetch related items
- Optimizes performance and reduces complexity
Full Transcript
Single-table design methodology in DynamoDB means storing multiple entity types in one table. We assign a partition key (PK) to group related items, like all data for one user. The sort key (SK) differentiates item types and orders them. For example, a user profile has SK 'PROFILE#123', and orders have SK starting with 'ORDER#'. This lets us query all user data or just orders efficiently. Attributes hold the actual data. This design reduces the need for multiple tables and complex joins, improving speed and simplicity.

Practice

(1/5)
1. What is the main advantage of using single-table design in DynamoDB?
easy
A. It requires multiple tables for each data type to keep data separate.
B. It allows storing different types of related data in one table for faster queries.
C. It automatically creates indexes for all attributes without setup.
D. It stores data only in JSON format for easy reading.

Solution

  1. Step 1: Understand single-table design purpose

    Single-table design stores multiple related data types in one table to reduce the number of queries needed.
  2. Step 2: Compare options with this purpose

    It allows storing different types of related data in one table for faster queries. matches this purpose. Options B, C, and D describe incorrect or unrelated features.
  3. Final Answer:

    It allows storing different types of related data in one table for faster queries. -> Option B
  4. Quick Check:

    Single-table design = faster queries with one table [OK]
Hint: Single-table design means one table for related data [OK]
Common Mistakes:
  • Thinking single-table means multiple tables
  • Assuming automatic index creation
  • Confusing data format with design method
2. Which of the following is the correct way to define a partition key in a DynamoDB table using single-table design?
easy
A. PartitionKey: orderId, SortKey: userId
B. PartitionKey: userId#orderId
C. PartitionKey: userId, SortKey: orderId
D. PartitionKey: orderId#userId

Solution

  1. Step 1: Recall key structure in single-table design

    Single-table design uses a partition key and a sort key to organize data efficiently.
  2. Step 2: Identify correct key assignment

    PartitionKey: userId, SortKey: orderId correctly assigns userId as partition key and orderId as sort key, which is common for grouping orders by user.
  3. Final Answer:

    PartitionKey: userId, SortKey: orderId -> Option C
  4. Quick Check:

    Partition and sort keys = userId and orderId [OK]
Hint: Partition key groups items; sort key orders them [OK]
Common Mistakes:
  • Using combined keys as partition key only
  • Swapping partition and sort keys incorrectly
  • Using concatenated keys without sort key
3. Given a DynamoDB table with partition key PK and sort key SK, which query will fetch all orders for user with ID 'user123' if orders are stored with PK = 'USER#user123' and SK = 'ORDER#orderId'?
medium
A. Query with KeyConditionExpression: PK = 'ORDER#orderId' AND SK = 'USER#user123'
B. Query with KeyConditionExpression: PK = 'USER#user123' AND SK = 'ORDER#orderId'
C. Scan with FilterExpression: PK = 'USER#user123'
D. Query with KeyConditionExpression: PK = 'USER#user123' AND SK begins_with 'ORDER#'

Solution

  1. Step 1: Understand key structure for orders

    Orders have PK as 'USER#user123' and SK starting with 'ORDER#', so all orders share the same PK but different SKs.
  2. Step 2: Identify query to fetch all orders for user

    Query with KeyConditionExpression: PK = 'USER#user123' AND SK begins_with 'ORDER#' queries PK equal to 'USER#user123' and SK that begins with 'ORDER#', which fetches all orders for that user.
  3. Final Answer:

    Query with KeyConditionExpression: PK = 'USER#user123' AND SK begins_with 'ORDER#' -> Option D
  4. Quick Check:

    PK fixed, SK prefix filter = all user orders [OK]
Hint: Use begins_with on sort key to get related items [OK]
Common Mistakes:
  • Swapping PK and SK in query
  • Using scan instead of query
  • Filtering SK with exact match instead of prefix
4. You wrote this query to get all comments for a post in a single-table design DynamoDB table:
KeyConditionExpression: PK = 'POST#123' AND SK = 'COMMENT#'

Why does this query return no results?
medium
A. Because SK uses '=' instead of 'begins_with' to match multiple comments.
B. Because PK should be 'COMMENT#' and SK should be 'POST#123'.
C. Because the table does not support queries on SK.
D. Because the query syntax is missing a FilterExpression.

Solution

  1. Step 1: Analyze the KeyConditionExpression

    The query uses SK = 'COMMENT#', which looks for an exact match on SK.
  2. Step 2: Understand why no results appear

    Comments likely have SK starting with 'COMMENT#' plus an ID, so exact match fails. Using 'begins_with' on SK is needed to get all comments.
  3. Final Answer:

    Because SK uses '=' instead of 'begins_with' to match multiple comments. -> Option A
  4. Quick Check:

    Use begins_with for multiple SK matches [OK]
Hint: Use begins_with for partial sort key matches [OK]
Common Mistakes:
  • Using '=' instead of begins_with on SK
  • Swapping PK and SK in query
  • Assuming FilterExpression fixes key condition
5. You want to design a single DynamoDB table to store users, their orders, and order items. Which key design best supports fetching all orders and items for a user efficiently?
hard
A. PartitionKey: USER#userId, SortKey: ORDER#orderId for orders; PartitionKey: ORDER#orderId, SortKey: ITEM#itemId for items
B. PartitionKey: userId, SortKey: orderId#itemId for all data types
C. PartitionKey: USER#userId, SortKey: ORDER#orderId#ITEM#itemId for all items and orders
D. PartitionKey: ITEM#itemId, SortKey: ORDER#orderId for all data

Solution

  1. Step 1: Understand data grouping needs

    We want to fetch all orders and items for a user efficiently, so grouping orders by user and items by order is key.
  2. Step 2: Evaluate key designs

    PartitionKey: USER#userId, SortKey: ORDER#orderId for orders; PartitionKey: ORDER#orderId, SortKey: ITEM#itemId for items uses USER#userId as partition key for orders and ORDER#orderId as partition key for items, allowing efficient queries by user and order. Other options mix keys or combine too much, making queries inefficient.
  3. Final Answer:

    PartitionKey: USER#userId, SortKey: ORDER#orderId for orders; PartitionKey: ORDER#orderId, SortKey: ITEM#itemId for items -> Option A
  4. Quick Check:

    Group orders by user, items by order = PartitionKey: USER#userId, SortKey: ORDER#orderId for orders; PartitionKey: ORDER#orderId, SortKey: ITEM#itemId for items [OK]
Hint: Group data by parent keys for efficient queries [OK]
Common Mistakes:
  • Using one partition key for all data types
  • Combining all keys into one long sort key
  • Using item ID as partition key for all data
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard