2015-11-14 35 views
1

我最近开始学习DynamoDB和创建表“评论”具有以下属性(与DynamoDB类型一起):主键和GSI设计在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 

最后一个索引有点瑕疵,因为我只在表中引入了'active'属性,因此所有记录的值都可以是'1',我可以将它用作GSI的Hash Key。

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

回答

0

我觉得你的设计是正确的:

  • 点2表键,GSI将覆盖你的前两个查询。这里没有惊喜,这是非常标准的。
  • 我认为你的最后一次查询的设计是正确的,即使有点黑客,并且可能不是性能最好的。考虑到DynamoDB限制,您需要使用相同的哈希键值。您希望能够按顺序获取值,因此您需要使用范围键。由于您只想使用范围键,因此您需要为散列键提供相同的值。你应该注意到,当你的表增长到很多分区时,这个可能不能很好地扩展(虽然我没有任何数据来支持这个语句)。