You have a DynamoDB table with a Global Secondary Index (GSI) that uses the attribute GSI1PK as the partition key and GSI1SK as the sort key. The table uses GSI overloading where GSI1PK stores different entity types (e.g., 'USER#123', 'ORDER#456').
What will be the output of this query?
Query GSI1 where GSI1PK = 'USER#123' and GSI1SK begins with 'PROFILE#'
Assuming the table has these items:
- {GSI1PK: 'USER#123', GSI1SK: 'PROFILE#MAIN', Data: 'UserProfileData'}
- {GSI1PK: 'USER#123', GSI1SK: 'ORDER#789', Data: 'OrderData'}
- {GSI1PK: 'ORDER#456', GSI1SK: 'PROFILE#MAIN', Data: 'OrderProfileData'}
Remember the query filters on GSI1PK and the sort key prefix.
The query filters items where GSI1PK equals 'USER#123' and GSI1SK starts with 'PROFILE#'. Only the first item matches both conditions.
What is the main advantage of using the GSI overloading technique in DynamoDB?
Think about how overloading helps organize different data types in one index.
GSI overloading uses a shared partition key attribute with prefixes to store different entity types in the same GSI, enabling efficient queries across multiple types.
Which of the following DynamoDB query expressions correctly queries a GSI named GSI1 to find all items with GSI1PK = 'ORDER#' and GSI1SK greater than '2023-01-01'?
Remember that DynamoDB KeyConditionExpression supports equality on partition key and range conditions on sort key.
DynamoDB requires the partition key to be matched exactly in KeyConditionExpression. Using begins_with on partition key is invalid. The correct syntax uses equality on GSI1PK and a range condition on GSI1SK.
You have a GSI overloaded with multiple entity types distinguished by prefixes in GSI1PK. Queries on one entity type are slow because the GSI contains many items of other types.
Which optimization will improve query performance?
Think about how to reduce the number of items scanned during the query.
Using a sort key prefix in the query narrows the search range within the overloaded GSI, reducing scanned items and improving performance. Filters apply after fetching and do not reduce read cost. Separate GSIs increase cost and complexity. Increasing RCUs helps throughput but not query efficiency.
A query on a DynamoDB GSI overloaded with multiple entity types returns no results, even though matching items exist. The query uses begins_with(GSI1PK, :prefix) with :prefix = 'USER#'.
What is the most likely cause of the problem?
Recall DynamoDB query restrictions on partition keys.
DynamoDB requires the partition key in KeyConditionExpression to be an exact match. Using begins_with on the partition key causes the query to return no results.