Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between partition key and sort key in amazon dynamodb?

With contrast to primary key, composite key and candidate key, what is a partition key and a sort key in dynamodb?

like image 871
Samarth Juneja Avatar asked May 16 '19 10:05

Samarth Juneja


People also ask

What is difference between partition key and sort key DynamoDB?

Partition key and sort key – Referred to as a composite primary key, this type of key is composed of two attributes. The first attribute is the partition key, and the second attribute is the sort key. DynamoDB uses the partition key value as input to an internal hash function.

What is a DynamoDB partition key?

Partition key: A simple primary key, composed of one attribute known as the partition key. Attributes in DynamoDB are similar in many ways to fields or columns in other database systems. Partition key and sort key: Referred to as a composite primary key, this type of key is composed of two attributes.

What are the two different primary keys supported in a DynamoDB table?

There are two types of primary keys in DynamoDB: Partition key: This is a simple primary key. If the table has only a partition key, then no two items can have the same partition key value. Composite primary key: This is a combination of partition key and sort key.

Why do we need sort key in DynamoDB?

In an Amazon DynamoDB table, the primary key that uniquely identifies each item in the table can be composed not only of a partition key, but also of a sort key. Well-designed sort keys have two key benefits: They gather related information together in one place where it can be queried efficiently.


1 Answers

The partition key is used for partitioning the data. Data with the same partition key is stored together, which allows you to query data with the same partition key in 1 query.

The (optional) sort key determines the order of how data with the same partition key is stored. Using a clever sort key allows you to query many items in 1 query.

An example: let's say I'm storing logging data for several applications. My partition key could be the Application Name, and the sort key the timestamp of the log. This allows me to query all logs of a particular application of the last hour in 1 query, using the BEGINS WITH operator, or even all the logs of last Wednesday for an application, by using the BETWEEN operator.

The partition key + the optional sort key form the primary key of the table, so they must be unique. Additionally, they are immutable.

The choice of your partition key and sort key should be based on your most important access pattern. If you have other access patterns, you can accommodate for them by using Global Secondary Indexes, but this comes with a cost.

like image 196
wvdz Avatar answered Sep 23 '22 04:09

wvdz