📜  DynamoDB-索引

📅  最后修改于: 2020-11-28 14:01:42             🧑  作者: Mango


DynamoDB使用索引作为主键属性来改善访问。它们加快了应用程序访问和数据检索的速度,并通过减少应用程序延迟来支持更好的性能。

次要指标

次要索引包含属性子集和备用键。您可以通过针对索引的查询或扫描操作来使用它。

其内容包括您投影或复制的属性。在创建时,您可以为索引定义一个备用键,并定义要投影到索引中的所有属性。然后,DynamoDB将属性复制到索引中,包括从表中获取的主键属性。执行完这些任务后,您只需像对表一样执行查询/扫描。

DynamoDB自动维护所有二级索引。在项操作(例如添加或删除)上,它将更新目标表上的所有索引。

DynamoDB提供两种类型的二级索引-

  • 全局二级索引-该索引包括分区键和排序键,它们可能与源表不同。由于对索引的查询/扫描具有跨所有表数据以及跨所有分区的功能,因此它使用标签“ global”。

  • 本地二级索引-该索引与表共享一个分区键,但使用不同的排序键。其“本地”性质是由于其所有分区都作用于具有相同分区键值的表分区。

最佳索引类型取决于应用程序需求。考虑下表中呈现的两者之间的区别-

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不允许并发创建。

每个二级索引都需要某些规范-

  • 类型-指定本地或全局。

  • 名称-它使用与表相同的命名规则。

  • 密钥架构-仅允许顶级字符串,数字或二进制类型,而索引类型确定其他要求。

  • 投影属性-DynamoDB自动投影它们,并允许任何数据类型。

  • 吞吐量-指定全局二级索引的读取/写入容量。

每个表的索引限制为5个全局和5个本地。

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

注意-这些值每6小时更新一次。

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

–删除表还会删除所有索引。