Core constructs/concepts of DynamoDB
- Table - A collection of items (data) in DynamoDB.
- Items - A collection/group of attributes within a table. (Items in DynamoDB are similar in many ways to rows, records, or tuples in other database systems.)
- Attributes- An attribute is a fundamental data element, something that does not need to be broken down any further.
- DynamoDB supports nested attributes up to 32 levels deep.
Let’s relate above constructs with Document Databases
- Collection - A collection where you can store documents, similar to Table in DynamoDB.
- Document - An individual record within a collection, similar to an Item in DynamoDB.
- Fields - Data fields within a document, equivalent to attributes in DynamoDB.
Two types of keys in an Item.
-
Partition key - also called a hash key
- DynamoDB uses the partition key’s value as input to an internal hash function. The output from the hash function determines the partition (physical storage internal to DynamoDB) in which the item will be stored.
-
Sort key - also called a range key
- DynamoDB uses the partition key value as input to an internal hash function. The output from the hash function determines the partition (physical storage internal to DynamoDB) in which the item will be stored. All items with the same partition key value are stored together, in sorted order by sort key value.
Primary Key
- When you create a table, in addition to the table name, you must specify the primary key of the table.
- The primary key uniquely identifies each item in the table, so that no two items can have the same key.
DynamoDB supports two different kinds of primary keys
- Partition key – A simple primary key, composed of one attribute known as the partition key.
- 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.
Secondary indexes
- You can create one or more secondary indexes on a table. A secondary index lets you query the data in the table using an alternate key, in addition to queries against the primary key. DynamoDB doesn’t require that you use indexes, but they give your applications more flexibility when querying your data.
DynamoDB supports two kinds of indexes:
- Global secondary index – An index with a partition key and sort key that can be different from those on the table.
- Local secondary index – An index that has the same partition key as the table, but a different sort key.
References: