DynamoDB – Indexes

DynamoDB uses indexes for primary key attributes to improve accesses. They accelerate application accesses and data retrieval, and support better performance by reducing application lag.

Secondary Index

A secondary index holds an attribute subset and an alternate key. You use it through either a query or scan operation, which targets the index.

Its contents include attributes you project or copy. In creation, you define an alternate key for the index, and any attributes you wish to project in the index. DynamoDB then performs a copy of the attributes into the index, including primary key attributes sourced from the table. After performing these tasks, you simply use a query/scan as if performing on a table.

DynamoDB automatically maintains all secondary indices. On item operations, such as adding or deleting, it updates any indexes on the target table.

DynamoDB offers two types of secondary indexes βˆ’

  • Global Secondary Index βˆ’ This index includes a partition key and sort key, which may differ from the source table. It uses the label β€œglobal” due to the capability of queries/scans on the index to span all table data, and over all partitions.
  • Local Secondary Index βˆ’ This index shares a partition key with the table, but uses a different sort key. Its β€œlocal” nature results from all of its partitions scoping to a table partition with identical partition key value.

The best type of index to use depends on application needs. Consider the differences between the two presented in the following table βˆ’

QualityGlobal Secondary IndexLocal Secondary Index
Key SchemaIt uses a simple or composite primary key.It always uses a composite primary key.
Key AttributesThe index partition key and sort key can consist of string, number, or binary table attributes.The partition key of the index is an attribute shared with the table partition key. The sort key can be string, number, or binary table attributes.
Size Limits Per Partition Key ValueThey carry no size limitations.It imposes a 10GB maximum limit on total size of indexed items associated with a partition key value.
Online Index OperationsYou can spawn them at table creation, add them to existing tables, or delete existing ones.You must create them at table creation, but cannot delete them or add them to existing tables.
QueriesIt allows queries covering the entire table, and every partition.They address single partitions through the partition key value provided in the query.
ConsistencyQueries of these indices only offer the eventually consistent option.Queries of these offer the options of eventually consistent or strongly consistent.
Throughput CostIt includes throughput settings for reads and writes. Queries/scans consume capacity from the index, not the table, which also applies to table write updates.Queries/scans consume table read capacity. Table writes update local indexes, and consume table capacity units.
ProjectionQueries/scans can only request attributes projected into the index, with no retrievals of table attributes.Queries/scans can request those attributes not projected; furthermore, automatic fetches of them occur.

When creating multiple tables with secondary indexes, do it sequentially; meaning make a table and wait for it to reach ACTIVE state before creating another and again waiting. DynamoDB does not permit concurrent creation.

Each secondary index requires certain specifications βˆ’

  • Type βˆ’ Specify local or global.
  • Name βˆ’ It uses naming rules identical to tables.
  • Key Schema βˆ’ Only top level string, number, or binary type are permitted, with index type determining other requirements.
  • Attributes for Projection βˆ’ DynamoDB automatically projects them, and allows any data type.
  • Throughput βˆ’ Specify read/write capacity for global secondary indexes.

The limit for indexes remains 5 global and 5 local per table.

You can access the detailed information about indexes with DescribeTable. It returns the name, size, and item count.

Note βˆ’ These values updates every 6 hours.

In queries or scans used to access index data, provide the table and index names, desired attributes for the result, and any conditional statements. DynamoDB offers the option to return results in either ascending or descending order.

Note βˆ’ The deletion of a table also deletes all indexes.

This Post Has 2 Comments

  1. binary option robot 1 0 review times

    Informative article, just what I wanted to find.

  2. K Z

    Thanks a lot.

Leave a Reply