What makes DynamoDB so much more than just a simple Key-Value store is the secondary indexes. They allow you to quickly query and lookup items based on not only the primary index attributes, but also attributes of your choice. Secondary Indexes, unlike primary key, are not required, and they don't have to be unique. Generally speaking, they allow much more flexible query access patterns.
Different DynamoDB Indexes
Local Secondary Indexes use the same hash key as the primary index but allow you to use a different sort key. That also means that they can be created only on tables with composite primary key.
- Limit you to only 10GB of data per Hash/Partition Key.
- Unlike GSIs, they share throughput with base table - if you query for data using LSI, the usage will be calculated against capacity of the underlying table and its base index.
- They have to be specified at table creation - you can't add or remove them after provisioning the table.
Global Secondary Keys are much more flexible and don't have LSI limitations. They don't have to share the same hash key, and they can also be created on a table with a simple key schema. Most of the time, you'll find yourself using GSIs over LSIs because they enable much more flexible query access patterns.
- Don't have that 10GB of data per Hash/Partition Key limit like LSIs have.
- They don't share throughput with the base table. Each of the GSIs is billed independently, and throttling is also separated as a consequence.
- GSIs can be altered after the table has been created. You can add and delete them whenever you want. Moreover, if you create GSI on the attribute already defined in a collection of items, the index will also be backfilled with that data.
- They are eventually consistent - if you're writing an item to a table, it is asynchronously propagated to the rest of GSIs. It means that query results might sometimes not be consistent and you should be aware of that when creating your application.
Frequently Asked Questions
What is the difference between primary index and secondary index in DynamoDB?
The key difference is that the primary index has to exist while the secondary indexes are optional. Primary index uniquely identifies each of the items in the table while secondary indexes only allow you to query them using different attributes.
Does DynamoDB have index based throttle?
Yes and no. Because GSIs have their own and separate throughput, the throttle on these is also independent of the base index. On the other hand, LSIs don't have index-based throttling. They share both throughput and throttles with other LSIs and base index.
How many secondary indexes can you create per table in DynamoDB?
You can only create 20 global secondary indexes and 5 local secondary indexes per table.
What is the DynamoDB index cost?
DynamoDB Indexes are free of charge.
© 2021 Dynobase