Introduction
A GSI key selection strategy helps you choose the best keys for your Global Secondary Index to find data quickly and efficiently.
Create a GSI with:
- Partition key (hash key): attribute to group items
- Sort key (optional): attribute to sort items within a group
Example:
CREATE GLOBAL SECONDARY INDEX index_name ON table_name (partition_key, sort_key);Partition key only:
PartitionKey = "Category"Partition key and sort key: PartitionKey = "UserId" SortKey = "OrderDate"
Partition key with high cardinality: PartitionKey = "TransactionId"
CREATE TABLE Orders ( OrderId STRING PRIMARY KEY, UserId STRING, OrderDate STRING, Category STRING ); CREATE GLOBAL SECONDARY INDEX UserOrdersIndex ON Orders ( UserId, OrderDate );
Category and a sort key named Timestamp in a DynamoDB table creation JSON?UserId and the sort key is OrderDate, what will be the result of this query?Query on GSI with UserId = 'user123' and OrderDate between '2023-01-01' and '2023-01-31'
Category and sort key Price. Your queries on this GSI are slow and uneven. What is the most likely cause?CustomerId and sort them by OrderStatus and then by OrderDate. Which key selection strategy is best?Pass the test and this pattern moves unknown