DynamoDB Composite Key - The Ultimate Guide [w/ Code Examples]
Written by Charlie Fish
Published on September 25th, 2022
Time to 10x your DynamoDB productivity with Dynobase [learn more]
What is a Composite Key in DynamoDB?
A composite key in DynamoDB is made up of both the partition key and the sort key. Unlike other attributes, the composite key does not actually exist within the item. It is an abstract concept that can be thought of as a combination of the partition key and sort key. Therefore it will not be visible or returned in the item itself. When you have a sort key, you will always have a composite key.
When you have a composite key on your DynamoDB Table, that will be considered the primary key. In simpler terms, this means when you have a sort key set on your table, all Get, Update, Delete item commands must include both the partition key and sort key. This is because the partition key is no longer considered unique, the composite key is considered unique, and therefore you need both elements to identify the specific item you are referring to.
Why Do I Need a Composite Key in DynamoDB?
A composite key allows you to further query your data in an efficient manner. While you can query just based on the partition key, sometimes your partition will contain many items, which would start to degrade the performance of your queries. By adding a sort key, you can further query your data in a more efficient manner.
When querying, you must specify the partition key equal to something. However, the power of the composite key is that you can further filter the results by the sort key with more flexible comparison operations (greater than, less than, begins with, etc).
The Best Practices You Should Follow For DynamoDB Composite Keys
As with other keys, it is critical that you define your keys correctly. Dynobase has a world-class Single Table Design Tool that can help you visualize access patterns and how to design your keys in an optimal way based on your access patterns. With this tool, you can see exactly how queries will interact with your data based on the key values you define.
Composite Key vs Other Key Types
Composite Key vs Primary Key
If you are using the composite key on a DynamoDB Table itself, it is considered the primary key for the table. However, if you are using the composite key on a global index, it is not considered the primary key for the table.
Composite Key vs Partition Key
The composite key is made up of both the partition key and sort key. It only applies if you have a sort key on your table. If you do not have a sort key, then you do not have a composite key.
Working with Composite Key (Code Examples)
Create Composite Key
Creating a composite key is as simple as defining both the partition key and the sort key on the item you add to your table.
Query on Composite Key
This will query your database and return all/multiple items with the partition key (
pk) attribute equal to
Get Item by Composite Key
This will get a single item where the partition key (
pk) attribute is equal to
myPartitionKey, and the sort key (
sk) attribute is equal to
mySortKey. Remember, the composite key is unique, so only a single item can ever exist in your table that matches this key.
You can not query on just the partition key if you have a composite key. You must always include both the entire composite key. If you want to query just based on the partition key, it can lead to many items being returned, and therefore you must use the Query on Composite Key code sample above.
Frequently Asked Questions
How to store a composite key in DynamoDB?
You can store a composite key in DynamoDB by defining both the partition key and sort key on the item you add to your table.
Can I create a DynamoDB composite key using 3 attributes?
You can not create a DynamoDB composite key using 3 attributes/columns. You can of course set your application to combine multiple attributes into a single field and use that as your partition key or sort key. However, this functionality is not built into DynamoDB and would all be handled at your application level.
Can a DynamoDB composite key be a primary key?
Yes. If you have a composite key on your DynamoDB Table itself, it will be considered the primary key.