1

我最近开始学习 DynamoDB 并创建了一个具有以下属性的表“评论”(以及 DynamoDB 类型):

productId - String
username - String
feedbackText - String
lastModifiedDate - Number (I'm storing the UNIX timestamp)
createdDate - Number
active - Number (0/1 value, 1 for all records by default)

以下是我希望在此表上运行的查询:

1. Get all reviews for a 'productId'
2. Get all reviews submitted by a 'username' (sorted asc/desc by lastModifiedDate)
3. Get N most recent reviews across products and users (using lastModifiedDate)

现在,为了能够运行这些查询,我在“评论”表上创建了以下内容:

1. A Primary Key with 'productId' as the Hash Key and 'username' as the Range Key
2. A GSI with 'username' as the Hash Key and 'lastModifiedDate' as the Range Key
3. A GSI with 'active' as the Hash Key and 'lastModifiedDate' as the Range Key

最后一个索引有点像 hack,因为我只在表中引入了“活动”属性,因此所有记录的值都可以是“1”,我可以将它用作 GSI 的哈希键。

我的问题很简单。我已经阅读了一些关于 DynamoDB 的内容,这是我能想到的最好的设计。我想问是否有更好的主键/索引设计可以在这里使用。如果 DynamoDB 中有一个我可能错过的概念,它可能对这个特定用例有益。谢谢!

4

1 回答 1

0

我认为您的设计是正确的:

  • 第 2 点中的表键和 GSI 将涵盖您的前两个查询。这里没有惊喜,这是相当标准的。
  • 我认为您对最后一个查询的设计是正确的,即使有点 hacky 并且在性能方面可能不是最好的。考虑到 DynamoDB 的限制,您需要为哈希键使用相同的值。您希望能够按顺序获取值,因此您需要使用范围键。由于您只想使用范围键,因此您需要为哈希键提供相同的值。您应该注意,当您的表增长到许多分区时,这可能无法很好地扩展(尽管我没有任何数据来支持该语句)。
于 2015-11-15T23:59:09.170 回答