What are partition keys and sort keys in DynamoDB?
Short answer
A partition key is a required attribute that DynamoDB uses to determine which physical partition stores a given item, while an optional sort key lets you store multiple items under the same partition key, sorted by that key's value, together forming what is called a composite…
Advertisement
AModel answer
A partition key is a required attribute that DynamoDB uses to determine which physical partition stores a given item, while an optional sort key lets you store multiple items under the same partition key, sorted by that key's value, together forming what is called a composite primary key, which enables efficient queries for related groups of items.
aws dynamodb create-table --table-name Orders --attribute-definitions AttributeName=CustomerId,AttributeType=S AttributeName=OrderDate,AttributeType=S --key-schema AttributeName=CustomerId,KeyType=HASH AttributeName=OrderDate,KeyType=RANGEReal-world example
An online store uses CustomerId as the partition key and OrderDate as the sort key for its Orders table, allowing it to quickly retrieve all orders for a specific customer sorted by date.
Likely follow-ups
- How do you choose a good partition key to avoid hot partitions?
- Can you change the primary key of a table after it is created?
Related topics