Partition Key vs Sort Key in DynamoDB: Key Differences and Usage
partition key uniquely identifies the partition where data is stored and determines data distribution, while the sort key organizes data within that partition and allows sorting and range queries. Together, they form a composite primary key that enables efficient querying and data retrieval.Quick Comparison
This table summarizes the main differences between the partition key and sort key in DynamoDB.
| Factor | Partition Key | Sort Key |
|---|---|---|
| Purpose | Determines data partition and distribution | Sorts and organizes data within a partition |
| Uniqueness | Must be unique per item if no sort key | Combined with partition key to ensure uniqueness |
| Data Distribution | Distributes data across partitions | Does not affect data distribution |
| Querying | Used to locate partition quickly | Used for range queries and sorting within partition |
| Required | Always required | Optional, only for composite keys |
| Example Usage | User ID | Timestamp of user activity |
Key Differences
The partition key is the primary attribute that DynamoDB uses to distribute data across multiple storage nodes. It ensures that data is spread evenly to optimize performance and scalability. Each item with the same partition key is stored together in the same partition.
The sort key is optional and works alongside the partition key to create a composite primary key. It allows multiple items to share the same partition key but be uniquely identified by the sort key. This enables sorting and querying items within the same partition based on the sort key's value.
Using both keys together allows efficient queries such as retrieving all items for a partition key within a certain range of sort key values, which is useful for time-series data or ordered records.
Partition Key Example
This example shows how to create a DynamoDB table with only a partition key and add an item.
aws dynamodb create-table \
--table-name Users \
--attribute-definitions AttributeName=UserID,AttributeType=S \
--key-schema AttributeName=UserID,KeyType=HASH \
--provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5
aws dynamodb put-item \
--table-name Users \
--item '{"UserID": {"S": "user123"}, "Name": {"S": "Alice"}}'
aws dynamodb get-item \
--table-name Users \
--key '{"UserID": {"S": "user123"}}'Sort Key Equivalent
This example creates a DynamoDB table with a composite primary key using both partition and sort keys, then adds and queries items.
aws dynamodb create-table \
--table-name UserActivities \
--attribute-definitions AttributeName=UserID,AttributeType=S AttributeName=Timestamp,AttributeType=N \
--key-schema AttributeName=UserID,KeyType=HASH AttributeName=Timestamp,KeyType=RANGE \
--provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5
aws dynamodb put-item \
--table-name UserActivities \
--item '{"UserID": {"S": "user123"}, "Timestamp": {"N": "1650000000"}, "Activity": {"S": "login"}}'
aws dynamodb query \
--table-name UserActivities \
--key-condition-expression "UserID = :uid and Timestamp > :ts" \
--expression-attribute-values '{":uid":{"S":"user123"}, ":ts":{"N":"1640000000"}}'When to Use Which
Choose a partition key when you need to distribute data evenly across storage and uniquely identify items if no sort key is used. It is essential for scaling and performance.
Choose a sort key when you want to store multiple related items under the same partition key and need to query or sort them efficiently, such as time-based logs or ordered events.
Use both keys together for complex queries that require filtering or sorting within a group of related items.