16
votes

I've built an application and it currently has a fairly standard user table, as so:

int id, varchar email, varchar password

If I was to switch this to DynamoDB then how would I create this table?

If I use a hash key with the email address, then I'd not be able to offer the ability to update your email and if I used a hash to store the ID, then I'd need to use a scan which is expensive and restricted by a 1Mb limit.

Any advice please? Thanks, Mark

3
This appears to be a duplicate of stackoverflow.com/q/12920884/268898 - Jeff Walker Code Ranger
same issue for me - Jayani Sumudini

3 Answers

5
votes

Do you say that it would be expensive to use ID as hash because you need to filter by the email field?

If you need to filter your queries by an non-key column, you often ends creating an index for it.

DynamoDB has no built-in secondary index, but is quite simple to implement your own solution.

The main table could use ID as hash, as you pointed, and a differente table would serve as index, it could be:

varchar email, int id

Being email the hash key for the secundary table. If it's allowed to have multiple users with the same email, than you could use ID as range, to make things easier, otherwise a simple column would fit.

2
votes

Having a different table for indexing will result in high maintenance. I came across a redundant model from my ex CTO.

For Your table : USER

RDBMS:

id, email password

1, [email protected],asks

DynamoDB:

KEY, id, email, password

1, 1, [email protected], asks

[email protected],1 , [email protected], asks

Instead of storing one record , you are storing redundantly to fetch using non indexed columns.

Hope the solution is clear.

1
votes

Create a User table with hash key id(user UUID) and other attributes. Create Global Secondary index with hash key Email and you have the option to choose sort key according to your requirement(User active status, Type of user).

  • you can get them and update the user on UUID(user id)
  • Get user with emailId
  • Get user with active status without filter(On sort key)
UserTable:
   Type: AWS::DynamoDB::Table
   Properties:
     TableName: UserTable
     AttributeDefinitions:
       - AttributeName: id
         AttributeType: S
       - AttributeName: email
         AttributeType: S
       - AttributeName: status(optional sort key according to your requirement)
         AttributeType: S
     KeySchema:
       - AttributeName: id
         KeyType: HASH
     ProvisionedThroughput:
       ReadCapacityUnits: 5
       WriteCapacityUnits: 5
     GlobalSecondaryIndexes:
       - IndexName: UserDetail
         KeySchema:
           - AttributeName: email
             KeyType: HASH
           - AttributeName: status(option)
             KeyType: RANGE
         Projection:
           ProjectionType: ALL
         ProvisionedThroughput:
           ReadCapacityUnits: 5
           WriteCapacityUnits: 5