我最近开始学习 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 中有一个我可能错过的概念,它可能对这个特定用例有益。谢谢!