0
0
DynamoDBquery~10 mins

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

Choose your learning style9 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.