You have a DynamoDB table storing categories and subcategories. Each item has a PK as the parent category ID and a SK as the child category ID.
Which query will return all subcategories of the parent category with PK = 'CAT#100'?
TableName: Categories
PartitionKey: PK
SortKey: SK
Example item: { PK: 'CAT#100', SK: 'SUB#200', Name: 'Subcategory A' }Remember, Query operations use the partition key to efficiently find items.
Querying by the partition key PK returns all items with that parent ID. Scans are less efficient and not needed here. Querying by SK alone is invalid because SK is the sort key, not the partition key.
Which DynamoDB design pattern is best suited to model hierarchical data with multiple levels of parent-child relationships?
Think about how DynamoDB's single-table design supports hierarchical queries.
Using composite keys with PK as the root ancestor and SK encoding the path allows efficient queries for all descendants. Multiple tables or missing sort keys do not support hierarchical queries well. GSI on timestamp is unrelated.
What is wrong with this DynamoDB query KeyConditionExpression?
KeyConditionExpression: "PK = :pkval AND begins_with(SK, :skprefix)"
ExpressionAttributeValues: {":pkval": "CAT#100", ":skprefix": "SUB#"}Check the rules for KeyConditionExpression syntax in DynamoDB.
In DynamoDB, KeyConditionExpression supports equality condition on the partition key optionally connected with lowercase 'and' to one condition on the sort key using operators like begins_with. Uppercase 'AND' is invalid syntax.
You have a DynamoDB table with millions of hierarchical items. Queries for all descendants of a parent are slow. Which optimization will improve query performance?
Think about how sort keys and begins_with can limit query scope.
Encoding the full path in the sort key and querying with begins_with limits the query to relevant items efficiently. GSI on name won't help hierarchical queries. Scan is slow for large data. Storing all descendants in one item can cause size limits and slow reads.
Given this DynamoDB query:
KeyConditionExpression: "PK = :pkval and SK > :skval"
ExpressionAttributeValues: {":pkval": "CAT#100", ":skval": "SUB#100"}But no items are returned, even though items with SK values greater than "SUB#100" exist. What is the most likely cause?
Consider how string comparisons work in DynamoDB sort keys.
DynamoDB compares sort keys lexicographically (alphabetical order). If SK values are strings like "SUB#100", "SUB#2", lex order means "SUB#2" > "SUB#100" is false because '1' < '2' in string comparison. This causes unexpected query results.