DynamoDB仅索引一组特定的值

时间:2019-04-25 22:02:24

标签: amazon-dynamodb amazon-dynamodb-index

我的dynamoDB索引充斥着大量数据。我想选择可以建立索引的值,并避免为其余索引建立索引。这可能吗?

可以说,下面是示例项目:

parent item:
{
    "hashKey":"a1"
    "indexHashKey":"parentType"
    "indexRangeKey":"date1"

}

child item:
{
    "hashKey":"a2"
    "indexHashKey":"childType"
    "indexRangeKey":"date11"

}

在我的用例中,我总是会要求索引仅获取parentType记录。索引正在加载大量数据,因为childType也被索引了(多数民众赞成在本质上)。我想选择特定的值(让我们说“ parentType1”,“ parentType2”)来在dynamoDB中建立索引。 dynamoDB是否为此目的提供任何功能?

替代: 如果dynamoDB没有提供这种功能,那么我应该

* avoid storing the child type of the item. But it would be good to have the child type stored.

or 

* Maintain two different fields. One to store parent record type and another to store child record type. This looks ugly.

任何建议都会有所帮助。

1 个答案:

答案 0 :(得分:0)

要清楚,您要将父项和子项都存储在一个表中,并且希望表上的索引仅包含子项吗?这是您问题的正确表示吗?

如果您不希望DynamoDB表中的所有数据都在索引中,则需要设计sparse index,这是一个常规索引,其中为索引哈希和范围键指定的属性不是在表格中的每个项目上您的问题是您的“ indexHashKey”和“ indexRangeKey”属性位于所有父项和子项中,因此它们都显示在索引中。请记住,DynamoDB表中的项目可以具有不同的属性。至少,它们需要包含表的哈希键和排序键(如果表有一个),但是它们不必包含碰巧是附加到表的索引的键的属性。

请考虑将您的项目修改为仅在父项目上包括索引哈希和范围键属性。例如:

parent item:
{
    "hashKey":"a1"
    "parentIndexHashKey":"parentType"
    "parentIndexRangeKey":"date1"

}

然后您可以按父类型(例如,parentType ==“ parentType2”)对该索引进行查询,并仅返回该表中具有该类型的父项。

如果您还需要仅对子项运行类似的查询,则可以通过设置该索引的哈希值属性和仅对子项进行排序的键来创建仅包含子项的第二个稀疏索引。

child item:
{
    "hashKey":"a2"
    "childIndexHashKey":"childType"
    "childIndexRangeKey":"date11"
}

或者,您可以将父项和子项存储在单独的DynamoDB表中,这样子项就无法进入父索引并干扰查询。