DynamoDB - Indexes

DynamoDB - Indexes

DynamoDB uses indexes on primary key attributes to improve accessibility. They speed up application access and data retrieval, and support higher performance by reducing application latency.

secondary index

The secondary index contains a subset of attributes and an alternate key. You use it with a query or a scan operation that targets an index.

Its content includes attributes that you design or copy. When you create it, you define an alternate key for the index and any attributes you want to project into the index. DynamoDB then copies the attributes into the index, including the primary key attributes retrieved from the table. Once you've completed these tasks, you simply use the query/scan as if you were doing work on a table.

DynamoDB automatically maintains all secondary indexes. When performing operations on elements, such as adding or deleting, all indexes on the target table are updated.

DynamoDB offers two types of secondary indexes −

  • Global Secondary Index - This index includes a partition key and a sort key, which may be different from the original table. It uses the "global" label due to the ability of queries/scans on the index to cover all table data and all partitions.

  • Local secondary index - This index shares a partition key with the table, but uses a different sort key. Its "local" character is due to the fact that all its partitions are in the form of a table with the same partition key value.

Global Secondary Index - This index includes a partition key and a sort key, which may be different from the original table. It uses the "global" label due to the ability of queries/scans on the index to cover all table data and all partitions.

Local secondary index - This index shares a partition key with the table, but uses a different sort key. Its "local" character is due to the fact that all its partitions are in the form of a table with the same partition key value.

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

Qualitative Global Secondary Index Local secondary index
Key scheme It uses a simple or composite primary key. It always uses a composite primary key.
Key Attributes The index key and sort key can consist of string, number, or binary table attributes. The index partition key is an attribute that is shared with the table partition key. The sort key can be string, numeric, or binary table attributes.
Size limits per partition key value They have no size restrictions. This imposes a maximum limit of 10 GB on the total size of indexed items associated with a partition key value.
Online Index Operations You can create them when you create tables, add them to existing tables, or delete existing ones. You must create them when you create a table, but you cannot delete them or add them to existing tables.
Requests This allows you to run queries that cover the entire table and each partition. They address individual partitions via the partition key value specified in the request.
consistency Requests for these indexes offer only a final consensus. Queries from them offer options for ultimately sequential or strictly sequential.
Bandwidth It includes read and write bandwidth settings. Queries/scans consume capacity from the index, not from the table, which also applies to table write updates. Queries/scans consume tables to read. The table writes local index updates and uses table capacity units.
projection Queries/scans can only query for attributes projected into an index, without retrieving table attributes. Requests/checks can request those attributes that are not projected; moreover, automatic selections of them occur.

When creating multiple tables with secondary indexes, do so sequentially; i.e. create a table and wait until it reaches the ACTIVE state before creating another one and waiting again. DynamoDB does not allow concurrent creation.

Each secondary index requires certain specifications −

  • Type - Specify local or global.

  • Name - Uses naming conventions identical to tables.

  • Key scheme - Only the top-level string, number, or binary type is allowed, with the index type specifying other requirements.

  • Attributes for projection - DynamoDB automatically projects them and allows any data type.

  • Throughput - Specify the read/write capacity for global secondary indexes.

Type - Specify local or global.

Name - Uses naming conventions identical to tables.

Key scheme - Only the top-level string, number, or binary type is allowed, with the index type specifying other requirements.

Attributes for projection - DynamoDB automatically projects them and allows any data type.

Throughput - Specify the read/write capacity for global secondary indexes.

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

You can access detailed information about indexes using DescribeTable . Returns the name, size, and number of items.

Note. These values ​​are updated every 6 hours.

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

Note. Dropping a table also drops all indexes.