Dynamodb 简明教程
DynamoDB - Indexes
DynamoDB 使用主键属性的索引来改进访问。它们加速了应用程序访问和数据检索,并且通过减少应用程序滞后支持更好的性能。
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.
其内容包括您映射或复制的属性。在创建时,您为索引定义另一个键以及任何您希望在索引中映射的属性。然后,DynamoDB 将这些属性复制到索引中,包括源自表的主键属性。执行这些任务后,您只需使用查询/扫描,如同对表执行操作一样即可。
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 自动维护所有二级索引。对于诸如添加或删除这样的条目操作,它会更新目标表上的任何索引。
DynamoDB automatically maintains all secondary indices. On item operations, such as adding or deleting, it updates any indexes on the target table.
DynamoDB 提供两种类型的二级索引 -
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 −
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 |
It 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. |
Projection |
Queries/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. |
使用辅助索引创建多个表时,按顺序创建;也就是说,先创建一个表,然后等待它变为 ACTIVE 状态后,再创建另一个并再次等待。DynamoDB 不允许并发创建。
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.
索引的限制仍然是每个表 5 个全局索引和 5 个本地索引。
The limit for indexes remains 5 global and 5 local per table.
你可以使用 DescribeTable 访问有关索引的详细信息。它会返回名称、大小和项目计数。
You can access the detailed information about indexes with DescribeTable. It returns the name, size, and item count.
Note − 这些值每 6 小时更新一次。
Note − These values updates every 6 hours.
在用于访问索引数据的查询或扫描中,提供表和索引名称、结果所需的属性以及任何条件语句。DynamoDB 提供了以升序或降序返回结果的选项。
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 − 删除表也会删除所有索引。
Note − The deletion of a table also deletes all indexes.