Composite Key in DynamoDB: Definition and Usage Explained
composite key is made up of two parts: a partition key and a sort key. This combination uniquely identifies each item in a table and allows for more flexible queries within the same partition.How It Works
Think of a composite key in DynamoDB like a two-part address for your data. The first part, called the partition key, decides which storage section (partition) your data goes to. The second part, called the sort key, organizes the data within that partition in a specific order.
This setup is like having a street name (partition key) and a house number (sort key). Together, they make sure you can find exactly one house on that street. In DynamoDB, this means you can store multiple items with the same partition key but different sort keys, and still keep each item unique.
Using a composite key helps DynamoDB quickly find and sort data without scanning the whole table, making your queries faster and more efficient.
Example
This example shows a DynamoDB table where the composite key consists of UserID as the partition key and OrderID as the sort key. Each user can have multiple orders, and each order is uniquely identified by the combination of these two keys.
import boto3 from boto3.dynamodb.conditions import Key dynamodb = boto3.resource('dynamodb') table = dynamodb.create_table( TableName='Orders', KeySchema=[ {'AttributeName': 'UserID', 'KeyType': 'HASH'}, # Partition key {'AttributeName': 'OrderID', 'KeyType': 'RANGE'} # Sort key ], AttributeDefinitions=[ {'AttributeName': 'UserID', 'AttributeType': 'S'}, {'AttributeName': 'OrderID', 'AttributeType': 'S'} ], ProvisionedThroughput={ 'ReadCapacityUnits': 5, 'WriteCapacityUnits': 5 } ) table.wait_until_exists() # Adding an item response = table.put_item( Item={ 'UserID': 'user123', 'OrderID': 'order789', 'Product': 'Book', 'Quantity': 1 } ) # Querying all orders for user123 response = table.query( KeyConditionExpression=Key('UserID').eq('user123') ) print(response['Items'])
When to Use
Use a composite key in DynamoDB when you want to group related items under the same partition key but still keep each item unique with a sort key. This is useful for scenarios like:
- Storing multiple orders for the same user, where
UserIDis the partition key andOrderIDis the sort key. - Logging events by device, where the device ID is the partition key and the timestamp is the sort key to keep events in order.
- Organizing messages in a chat app, with the chat room as the partition key and message time as the sort key.
This design helps you efficiently query all items in a group or a specific item by both keys.
Key Points
- A composite key combines a partition key and a sort key to uniquely identify items.
- It allows multiple items to share the same partition key but differ in sort keys.
- Enables efficient queries within a partition, like retrieving all items or a range of items.
- Commonly used for grouping related data such as user orders, logs, or messages.