Dynamodb 简明教程

DynamoDB - Indexes

DynamoDB 使用主键属性的索引来改进访问。它们加速了应用程序访问和数据检索,并且通过减少应用程序滞后支持更好的性能。

Secondary 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 −

  1. 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.

  2. 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 −

Quality

Global Secondary Index

Local Secondary Index

Key Schema

It uses a simple or composite primary key.

It always uses a composite primary key.

Key Attributes

The 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 Value

They carry no size limitations.

It imposes a 10GB maximum limit on total size of indexed items associated with a partition key value.

Online Index Operations

You 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.

Queries

It allows queries covering the entire table, and every partition.

They address single partitions through the partition key value provided in the query.

Consistency

Queries of these indices only offer the eventually consistent option.

Queries of these offer the options of eventually consistent or strongly consistent.

Throughput Cost

它包含读写吞吐量设置。查询/扫描耗用索引的容量,而不是表的容量,这也适用于表写更新。

查询/扫描会耗用表读容量。表写入会更新本地索引,并耗用表容量单位。

Projection

查询/扫描只能请求映射到索引中的属性,而不能检索表属性。

查询/扫描可以请求未映射的那些属性;此外,会自动提取它们。

使用辅助索引创建多个表时,按顺序创建;也就是说,先创建一个表,然后等待它变为 ACTIVE 状态后,再创建另一个并再次等待。DynamoDB 不允许并发创建。

每个辅助索引都需要某些规范 −

  1. Type − 指定本地或全局。

  2. Name − 它使用与表相同的命名规则。

  3. Key Schema − 只允许最高级别的字符串、数字或二进制类型,而索引类型决定了其他要求。

  4. Attributes for Projection − DynamoDB 会自动映射它们,并允许任何数据类型。

  5. Throughput − 为全局辅助索引指定读/写容量。

索引的限制仍然是每个表 5 个全局索引和 5 个本地索引。

你可以使用 DescribeTable 访问有关索引的详细信息。它会返回名称、大小和项目计数。

Note − 这些值每 6 小时更新一次。

在用于访问索引数据的查询或扫描中,提供表和索引名称、结果所需的属性以及任何条件语句。DynamoDB 提供了以升序或降序返回结果的选项。

Note − 删除表也会删除所有索引。