DynamoDB primary key fields are called Partition Key and Sort Key. DynamoDB also support two types of secondary indexes, that allows to query on any attribute (column) other that primary key: Global Secondary Indexes (GSI) and Local Secondary Indexes (LSI).
Partition Key and Sort Key
A primary key for a DynamoDB table can either be a single-attribute partition key or a composite partition-sort key. A partition key can be used to uniquely identify items in a table. When you cannot uniquely identity using the partition key, you can use a composite key with partition key with a sort key.
DynamoDB uses partition key value as input to a hash function, which will return details of the partition (location) of the data item. A sort key is usually used to sort a range of values. So partition key was originally called as a Hash Key and Sort Key was called as a Range Key.
Partition Key and Sort Key Limits:
-
The minimum length of a partition key value is 1 byte. The maximum length is 2048 bytes.
-
There is no practical limit on the number of distinct partition key values, for tables or for secondary indexes.
-
The minimum length of a sort key value is 1 byte. The maximum length is 1024 bytes.
-
In general, there is no practical limit on the number of distinct sort key values per partition key value.
-
Exception: For tables with local secondary indexes, for every distinct partition key value, the total sizes of all table and index items cannot exceed 10 GB. This might constrain the number of sort keys per partition key value.
-
Secondary Indexes
Local secondary index (LSI)
LSI scoped to a base table partition that has the same partition key value.
You can only create local secondary indexes during table creation.
Global secondary index (GSI)
With GSI, queries on the index can span all of the data in the base table, across all partitions.
Can be created anytime from the Indexes tab of the table. They cannot be created or modified later.
Important Points (Exam Tips)
-
If you are only using partition key, it should be unique. When using both partition key and sort key, sort key should be unique. In this case, all items with the same partition key are stored together, sorted by the sort key.
-
Indexes has their own partition key and sort key. For LSI the partition key is same as table’s partition key and sort key is different. For GSI both partition key and sort key are different from table’s partition key and sort key
- heartin's blog
- Log in or register to post comments
Recent comments