Alternative indexing methods for flexible data access patterns
By default, every DynamoDB table buils an index of items based on the
primary-key attribute. Apart form that, it’s possible to create other indexes based on different attributes of the same items.
Two types of indexes are supported: local and global. The main differences are:
Local indexes can vary only the
sort-key, maintaining the same
partition-key as the base table, thus being useful only for different sorting patterns.
Global indexes support different attributes for both
In general, developers should avoid using Local indexes and prefer Global ones. Local indexes limit data volume to 10 Gb for any given
partition-key value, which defeats the high scalability purpose of DynamoDB. Global indexes, on the other hand, impose no restrictions.
When setting up an index, DynamoDB will allow three data projection options. A data projection is a definition of which item attributes are projected - or copied - into the index:
sort-keyitem attributes will be available in index
Keeping the number of indexes to a minimum is recommended. Instead of creating too many indexes, consider using one table and one index (or very few indexes) for all querying requirements. This is possible by writing additional items to support alternative access patterns[^1].
Beware that indexes will consume storage space and write resources from the base table. When an item is inserted or modified in a table, DynamoDB needs to update the associated index(es). Updating an item in a table with five indexes, for example, will consume six write requests: one for the base table and five for the indexes.
Although projecting all attributes in indexes can be convenient, it might not be the smartest decision in all cases. Projected attributes consume storage space. Depending on the size of a table, projections can become very expensive.
Consider cases when items are rarely accessed or when just a few attributes are needed frequently. In those scenarios, it might be better to project only the key-attributes, or a minimum set of frequently used attributes.
Whenever is needed, it is always possible to retrieve additional attributes from the base table. This will consume an extra read requet, but could be way cheaper than doubling storage permanently on a project-all index.
[^1] Refer to the Access Pattern Strategies page
Save time spent on debugging applications.
Increase development velocity and quality.
Get actionable insights to your infrastructure.