In what situation do you use Simple Hash Keys on D

2019-06-12 21:19发布

问题:

Simple Hash Keys seem to be too simple to write an article, while many write about Composite Hash/Range Keys, because Composite Hash/Range Keys are useful for many complex situations. But I believe that in common applications, many of tables should be designed with Simple Hash Keys. In what situation do you use Simple Hash Keys?

For example, when you design hierarchical models like following, how do you design primary keys for each table? (All tables other than Tenant have tenant_id as a field.)

  • Tenant
  • User
  • Team
  • Project
  • Task
  • Team Member

Idea.1

Only "Team Member" is designed by Composite Hash/Range Key. Others are designed by Simple Hash Key.

Idea. 2

  • Tenant is designed by Simple Hash Key.
  • User, Team and Project's primary key would be composite (tenant_id, sub_id).
  • Task's primary key would be composite ({tenant_id}_{project_range_key}, sub_id).
  • Team Member's primary key would be composite ({tenant_id}_{team_range_key}, {tenant_id}_{user_range_key}).

where sub_id can be a sequential number, created_at, or else.

Update

After I posted this question, I learned more about DynamoDB and its history, and I recognize my concern much clearly now.

Before GSI was released by Amazon, we had to design tables like "Idea 2" in order to query against "tenant_id". But now we can use GSI, so we can design tables (e.g. Team, Project or Task) using the combination of "Simple Hash Key and GSI". Is that right??

回答1:

You use single hash key table to describe an item. For examle, a user have many info like name, age, etc. You can create a user table with user_id as hashkey and all other info as attributes.

You use hash-range schema to describe a relationship. For example, one team can have multiple users. So you can create a Team-Member-Relationship table with team-id as hashkey and user-id as range key.

In other words, draw a relationship diagram with vertex being (user, team, project) and edge being their relationship (1-to-1, 1-to-many, many-to-many, etc). Then use Hashkey schema for vertex and Hash-Range schema for edge.

Thanks!

Erben



回答2:

I have replied to the same post you have on AWS forums here. Hopefully it is helpful to you. Please feel free to follow up with me on there, though both Stack Overflow and AWS Forums are actively monitored by DynamoDB experts. Thanks for your interest in DynamoDB!