What are some best practices to create a composite key for my DynamoDB table?

2 minute read
0

I want to know what best practices I can use to create a composite key for my Amazon DynamoDB table.

Short description

Use a partition key and sort key to create a unique composite key. You can use the same partition key values with different sort key values, but the new composite key must form a unique value.

Use the new composite key to run effective queries and distribute data over partitions in DynamoDB. For more information, see Best practices for using sort keys to organize data.

Resolution

Choose a unique partition key with high cardinality

The partition key for a DynamoDB table must be unique. If you choose a partition key with low cardinality, then you might affect how data is stored and distributed over different partitions in the table. For more information on data distribution in DynamoDB, see Partitions and data distribution.

Choose a sort key that works with the access patterns

To more efficiently query information, choose a sort key that gathers related information in one place. The sort key attribute that you choose can improve the item selection that the query retrieves. A composite sort key lets you define a hierarchical relationship in your data so that you can query at any level of the hierarchy.

Example composite sort key:

[building]#[department]#[team]#[manager]

For example, you can select employeeID as the partition key to have unique combinations all the way up to manager.

Related information

Choosing the right DynamoDB partition key

AWS OFFICIAL
AWS OFFICIALUpdated 6 months ago