DynamoDB 分区键是否必须是唯一的?
Does DynamoDB partition key have to be unique?
我想在 dynamodb 中为订单创建一个 table,每个订单由(用户 ID、日期、唯一 ID、产品和总计)组成,我想使用 userId 查询特定用户的订单按日期排序,我想知道如何选择分区键?它必须是独一无二的吗?如果是的话,我怎样才能使它独一无二?在 mongodb 中,我会根据用户 ID 对我的订单进行分片,我怎样才能使用 dynamodb 实现相同的效果?
假设您有一个用户 A 的用户 ID - 1 和用户 B 的用户 ID - 2
我假设 A 和 B 一样可以多次订购。
So you can design your table with a composite key ( combination of partition and sort key).
In a table that has a partition key and a sort key, it's possible for multiple items to have the same partition key value. However, those items must have different sort key values. All items with the same partition key value are stored together, in sorted order by sort key value.
分区键 + 排序键的组合将产生唯一的顺序。
这里的分区键将是 userID,而 orderId 将是唯一的。因此,您可以使用查询操作通过 userId 查询特定用户的所有订单。对于按日期排序,您可以使用 filter expressions in your query operation
如果过滤器表达式无法帮助您按日期排序,那么您需要根据 createdAt 字段(日期字段)使用单独的索引。注意:- 索引是昂贵的。
DynamoDB allows for the specification of secondary indexes to aid in this sort of query. Secondary indexes can either be global, meaning that the index spans the whole table across hash keys, or local meaning that the index would exist within each hash key partition, thus requiring the hash key to also be specified when making the query.
替代设计。
user userId as partition key and CreatedAt as sortKey, yes seconds difference will lead to a unique timestamp and then you can use key condition expression in your query.
Querying DynamoDB by date
我想在 dynamodb 中为订单创建一个 table,每个订单由(用户 ID、日期、唯一 ID、产品和总计)组成,我想使用 userId 查询特定用户的订单按日期排序,我想知道如何选择分区键?它必须是独一无二的吗?如果是的话,我怎样才能使它独一无二?在 mongodb 中,我会根据用户 ID 对我的订单进行分片,我怎样才能使用 dynamodb 实现相同的效果?
假设您有一个用户 A 的用户 ID - 1 和用户 B 的用户 ID - 2 我假设 A 和 B 一样可以多次订购。
So you can design your table with a composite key ( combination of partition and sort key). In a table that has a partition key and a sort key, it's possible for multiple items to have the same partition key value. However, those items must have different sort key values. All items with the same partition key value are stored together, in sorted order by sort key value.
分区键 + 排序键的组合将产生唯一的顺序。 这里的分区键将是 userID,而 orderId 将是唯一的。因此,您可以使用查询操作通过 userId 查询特定用户的所有订单。对于按日期排序,您可以使用 filter expressions in your query operation
如果过滤器表达式无法帮助您按日期排序,那么您需要根据 createdAt 字段(日期字段)使用单独的索引。注意:- 索引是昂贵的。
DynamoDB allows for the specification of secondary indexes to aid in this sort of query. Secondary indexes can either be global, meaning that the index spans the whole table across hash keys, or local meaning that the index would exist within each hash key partition, thus requiring the hash key to also be specified when making the query.
替代设计。
user userId as partition key and CreatedAt as sortKey, yes seconds difference will lead to a unique timestamp and then you can use key condition expression in your query. Querying DynamoDB by date