1

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??

2 Answers 2

2

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

10
  • Thank you Erben. In this case, Global Secondary Indexes are used to query on "item" tables, right?
    – Junya
    Mar 2, 2015 at 1:26
  • 1
    GSI is just another way to organize the same data in the base table. The reason you want another reprensentation of your data is to make query easier. Lets say your base table is user_id as hashkey and project_id as range key. It is good for getting all the projects that a given user is involved. But if you want to get all the members of a specific project, you will have to scan the base table (very inefficient). What you can do is to create a GSI with project id as hash key and user id as range key (reverse of the base table). You can query this GSI to get all the member of a project
    – Erben Mo
    Mar 2, 2015 at 2:53
  • 1
    using GSI on an item table is also fine I think. Lets say you have a user table with user_id as hashkey and age as an attribute. Now you want all the user with 20 < age <24. What you can do is create a GSI with age as hashkey and user id as range key. Then you can query this GSI to get all user with age between a range.
    – Erben Mo
    Mar 2, 2015 at 3:02
  • Thank you. I have well understood. So the practices have changed after GSI released, right? (BTW, I have updated the question above.)
    – Junya
    Mar 2, 2015 at 3:15
  • But there is an upper limit on the number of the number of GSI you can use (5 / 10?)
    – Erben Mo
    Mar 2, 2015 at 4:26
0

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!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.