Dynamodb Indexes Deep Dive
In SQL databases, an index is a data structure which allows you to perform fast queries on specific columns in a table. You select the columns that you want included in the index and run your searches on the index, rather than on the entire dataset.
In DynamoDB, two types of index are supported to help speed up your DynamoDB queries:
- Local Secondary Index
- Global Secondary Index
Local Secondary Index
- An index that can only be created when you are creating your table.
- You cannot add, remove, or modify it later
- It has the same Partition Key as your original table
- Has a different sort key
- Gives you a different view of your data, organized according to an alternative Sort Key
- Any queries based on this sort key are much faster using the index than the main table
- For example, in a Customer table, you might use the UserID as the Partition Key and the AccountCreationDate as the Sort Key and use that as your Local Secondary Index:
- Partition Key: UserID
- Sort Key: AccountCreationDate
Global Secondary Index
- You can create when you create your table, or add it later
- Different Partition Key as well as a different Sort Key, giving you a different view of the data
- Speeds up any queries related to this alternative Partition and Sort Key
- For Example, in the customer table, you might use a Partition Key of e-mail address and Sort Key of last log-in date
- Partition Key: EmailAddress
- Sort Key: LastLoginDate