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

2 minute read
0

I want to learn the best practices to create a composite key for my Amazon DynamoDB table.

Resolution

When you create a composite key for your DynamoDB table, it's a best practice to do the following:

  • Choose a unique partition key with high cardinality: The partition key for a DynamoDB table must be unique. Choosing a partition key with low cardinality might affect the way data is stored and distributed over different partitions in the table. Therefore, be sure to choose a partition key with a high cardinality. For more information on data distribution in DynamoDB, see Partitions and data distributions.
  • Choose a sort key that compliments access patterns: Choose a sort key that gathers related information together in one place so that the information can be queried efficiently. Carefully choosing the sort key attribute is important because it can significantly improve the selectivity of the items retrieved by a query. Also, a composite sort key allows you to define a hierarchical relationship in your data so that you can query at any level of the hierarchy

For example, a composite sort key must look similar to the following:

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

In this case, you can select employeeID as the partition key so that you can have unique combinations all the way up to manager.

For more information, see Best practices for using sort keys to organize data. The combination of partition key and sort key or composite sort key creates a new unique composite key that allows for effective querying and distribution of data over partitions in DynamoDB.

Consider the following when you design your composite key:

  • The combination of the partition key and sort key must form a unique value.
  • You can have repeating partition keys with different sort keys.

Related information

Choosing the right DynamoDB partition key

AWS OFFICIAL
AWS OFFICIALUpdated 2 years ago