You have a DynamoDB table named Orders with a partition key CustomerID and a sort key OrderID. Which query will return all orders for customer with ID 123?
Remember, queries use the partition key to efficiently find items.
Querying with CustomerID = '123' uses the partition key to get all orders for that customer efficiently. Scans are less efficient and filtering on sort key alone won't work.
You want to store blog posts and their comments in one DynamoDB table. Each post has many comments. Which key design best supports efficient retrieval of all comments for a post?
Think about grouping comments under the post they belong to.
Using PostID as partition key groups all comments for that post together. CommentID as sort key uniquely identifies each comment.
You want to get the 5 most recent comments for a post. Comments have a Timestamp attribute. How should you design the sort key to optimize this query?
DynamoDB sort keys are stored in ascending order by default.
Using Timestamp as sort key in ascending order allows querying with ScanIndexForward=false to get recent comments first.
You run this DynamoDB query to get comments for post p1:
KeyConditionExpression: "PostID = :pid AND CommentID > :cid"
ExpressionAttributeValues: {":pid": "p1", ":cid": "c0"}But it returns no items, even though comments exist. What is the likely cause?
KeyConditionExpression conditions apply only to partition and sort keys.
Conditions on attributes that are not partition or sort keys in KeyConditionExpression cause no results. CommentID must be the sort key to use > operator.
You want to store posts and their comments in one DynamoDB table using a single table design. Which pattern correctly models the one-to-many relationship?
Single table design uses composite sort keys to distinguish item types.
Using PostID as partition key groups all items for a post. Sort key prefixes like 'POST#' and 'COMMENT#' distinguish posts from comments and allow querying all related items.