In DynamoDB single-table design, what is the main reason to use composite primary keys (partition key + sort key)?
Think about how DynamoDB retrieves items efficiently when you query by partition key and sort key.
Composite primary keys combine partition key and sort key to uniquely identify items and allow queries to fetch related items by sorting within the partition.
Given a DynamoDB table with partition key 'PK' and sort key 'SK', which query will return all orders for customer 'C123' sorted by order date?
Assume items have PK='CUSTOMER#C123' and SK='ORDER#2023-01-01', 'ORDER#2023-02-01', etc.
Query:
{
TableName: 'Orders',
KeyConditionExpression: 'PK = :pk and begins_with(SK, :skprefix)',
ExpressionAttributeValues: {
':pk': 'CUSTOMER#C123',
':skprefix': 'ORDER#'
}
}Remember that begins_with works on the sort key to filter items within a partition.
The query filters items with PK='CUSTOMER#C123' and SK starting with 'ORDER#', returning all that customer's orders sorted by SK.
Which option contains a syntax error in the DynamoDB UpdateExpression?
UpdateExpression examples:
A: "SET #status = :newStatus, #count = #count + :inc" B: "SET #status = :newStatus ADD #count :inc" C: "SET #status = :newStatus, ADD #count :inc" D: "SET #status = :newStatus, #count = #count + :inc"
Check how SET and ADD clauses are combined in UpdateExpression.
In DynamoDB UpdateExpression, multiple actions like SET and ADD must be separated by spaces, not commas. Option A lacks the space between the SET and ADD actions.
You want to efficiently query all comments for a blog post and also fetch all posts by an author. Which single-table design pattern helps achieve both with minimal queries?
Think about how GSIs can support different query patterns in single-table design.
Using a composite key for comments and a GSI keyed by author allows efficient queries for both comments by post and posts by author.
A query on a DynamoDB single-table design returns no results when trying to fetch all orders for customer 'C789'. The query is:
{
TableName: 'Sales',
KeyConditionExpression: 'PK = :pk and SK BETWEEN :start and :end',
ExpressionAttributeValues: {
':pk': 'CUSTOMER#C789',
':start': 'ORDER#2023-01-01',
':end': 'ORDER#2023-12-31'
}
}What is the most likely reason for no results?
Check how the sort key values are stored and how BETWEEN compares strings.
If SK values do not include the 'ORDER#' prefix but the query uses it, the BETWEEN condition won't match any items.