Felix
Felix

Reputation: 4081

Querying DynamoDB with unique hash attributes?

I'm planning to have a User table with UserName as the hash key and a LastLoginDate attribute (among others).

I would like to be able to query the table for something like: All users that have not logged in for the last month.

How would I do this with DynamoDB?

I have been looking at local secondary indices, and thought of making LastLoginDate a secondary index. But how I understand the documentation, secondary indices only help order results for the same hash key, and in my case each user will have a unique UserName. Does this make such a secondary index pointless?

Thanks in advance!

Upvotes: 3

Views: 2151

Answers (3)

Bhavik Joshi
Bhavik Joshi

Reputation: 2677

You can Create GSI on LastLoginDate and apply your logic by Firing the Query to GSI. This might help to get result faster instead of scanning foe HASH key and checking for LastLoginDate and applying logic.

Upvotes: 2

Kristian Ačkar
Kristian Ačkar

Reputation: 945

My approach to this problem is to create hash key for example 'userType' which could be 'regularUser', 'admin' etc. UserName could be range key and LastLoginDate could be index.

Then you can query table for particular user by providing hash key 'regularUser' and range key 'some user name for example'. And when you want all users which depends on their last login time instead of range key 'UserName' use index 'LastLoginDate'.

Upvotes: -1

prestomation
prestomation

Reputation: 7440

You are correct, you must always query by HashKey, unless you do a full table scan.

Doing a fulltable scan, you can look at each and every entry in your table and compare their LastLoginDate. This can quickly become unscalable depending on how many users you have.

Upvotes: 3

Related Questions