DynamoDB, like other databases, uses indexes to allow you to quickly find data held in the database. Unlike relational databases, however, Dynamo's indexes cannot be used `JOIN` two related tables together.
# Key Types
1. [[#Partition Key (Hash Key)|Partition Key]]
2. [[#Sort Key]]
3. [[#Local Secondary Indexes (LSI)|Local Secondary Index]]
4. [[#Global Secondary Index (GSI)|Global Secondary Index]]
## Partition Key (Hash Key)
A partition key in DynamoDB (also known as a hash key) is the primary key of each item in a DynamoDB table. Dynamo uses this key to evenly distribute data across partitions for scalability and performance.
Every table in Dynamo **must** have a partition key defined. Partition keys are defined at table creation and cannot be changed. When querying for data you will *always* provide the partition key. (Unless you do a full table scan, which is not recommended.)
> [!info]
> The DynamoDB SDK calls the Partition Key a Hash Key. These names are synonymous.
## Sort Key
Within each partition, Dynamo organizes items by the optional Sort Key that you define. Sort keys are optional, but do need to be defined at table creation like the partition key.
In practice, you would use the sort key to find out something you don't know about the item defined by its partition key.
## Local Secondary Indexes (LSI)
> [!tldr] TL;DR
> *The data in a local secondary index is organized by the **same partition key** as the base table, but with a **different sort key**.*
A Local Secondary Index:
1. Maintains an alternate [[#Sort Key|sort key]] for a given [[#Partition Key (Hash Key)|partition key]] value.
2. Contains a copy of some or all of the attributes from its base table.
3. [[#Projections|Projects]] some or all attributes from the main index into it.
The data in a local secondary index is organized by the same partition key as the base table, but with a different sort key. This lets you access data items efficiently across this different dimension. For greater query or scan flexibility, you can create up to five local secondary indexes per table.
Every local secondary index must meet the following conditions:
- The partition key is the same as that of its base table.
- The sort key consists of exactly one scalar attribute.
- The sort key of the base table is projected into the index, where it acts as a non-key attribute.
### Projecting Attributes
When you create a secondary index, you need to specify the attributes that will be projected into the index. DynamoDB provides three different options for this:
| Option | Description |
| ----------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `KEYS_ONLY` | Each item in the index consists only of the table partition key and sort key values, plus the index key values. The `KEYS_ONLY` option results in the smallest possible secondary index. |
| `INCLUDE` | In addition to the attributes described in `KEYS_ONLY`, the secondary index will include other non-key attributes that you specify. |
| `ALL` | The secondary index includes all of the attributes from the source table. Because all of the table data is duplicated in the index, an `ALL` projection results in the largest possible secondary index. |
****
## Global Secondary Index (GSI)
# Projections
#dynamodb