使用现有数据在 DynamoDB 中创建新索引

Creating a new index in DynamoDB with existing data

我有疑问。例如,我在 DynamoDB table 上创建了一个索引并插入了三个元素。现在我的索引看起来像这样:

现在使用 table 中的三个元素,我在 DynamoDB 上创建了一个新索引,如下所示:

而我看到的是以下问题(我的理论):

第一个索引的大小有这三个项目,但第二个索引没有这三个元素。我做这个测试是因为我有一个包含 1500 万个项目的 table 并且需要重新创建或添加一个新索引。问题是如果索引没有已经存在的 table 的元素,这意味着如果我的应用程序使用新索引只能在创建索引后找到新的插入。

问题是:

我认为你的理论是错误的:

Backfilling: For each item in the table, DynamoDB determines which set of attributes to write to the index based on its projection (KEYS_ONLY, INCLUDE, or ALL). It then writes these attributes to the index. 来自:https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.OnlineOps.html#GSI.OnlineOps.Creating

创建全局索引后table不会造成数据丢失。但是稀疏索引可能会导致这种情况。 让我们举个例子:

假设您有一个 table,主键 = 电子邮件,并且您创建了一个以主键作为区域的全局索引(即使在插入一些数据之后)。

Item_1 : { email: test@test.com, region: USA} 将同时插入您的 table 和 GSI

Item_2 : { email: test@test.com, age: 21} 将仅插入您的 table

其实这是一个非常有用的功能,你可以在这里了解更多 https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-indexes-general-sparse-indexes.html