What are Global Secondary Indexes and Local Secondary Indexes in DynamoDB?
Short answer
A Global Secondary Index lets you query your table using a completely different partition key and sort key than the table's primary key, supporting flexible query patterns across the entire table, while a Local Secondary Index shares the same partition key as the table but…
Advertisement
AModel answer
A Global Secondary Index lets you query your table using a completely different partition key and sort key than the table's primary key, supporting flexible query patterns across the entire table, while a Local Secondary Index shares the same partition key as the table but allows a different sort key, letting you query items within the same partition in a different order.
aws dynamodb update-table --table-name Orders --attribute-definitions AttributeName=Status,AttributeType=S --global-secondary-index-updates '[{"Create":{"IndexName":"StatusIndex","KeySchema":[{"AttributeName":"Status","KeyType":"HASH"}],"Projection":{"ProjectionType":"ALL"}}}]'Real-world example
An order management system adds a Global Secondary Index on the order status field, allowing it to efficiently query all pending orders across every customer, something the original primary key structure could not support directly.
Likely follow-ups
- When must a Local Secondary Index be created compared to a Global Secondary Index?
- What are the cost implications of adding multiple indexes?
Related topics