2
votes

I store user accounts in DynamoDB:

{
  email: 'user1@xxx.com',
  expires: 1548807053247,
}

My hash key is the email field.

I want to add a daily cron job which will send a reminder email for all accounts about to expire (in the next 14 days).

For that, I need to query on expires field alone - without using the hash key.

I assume I need to define a secondary index on this field (probably global and not local?), but I'm not sure on how to write the proper query for it.

I'm using AWS.DynamoDB.DocumentClient for accessing the table, thanks in advance!

2
Whether you use a GSI or LSI depends on what level of consistency and performance you need. Also, LSIs are created with the table, stored with the table, and cannot be removed post table creation without deleting the table. GSIs, on the other hand, can be created and destroyed at will, but they are eventually consistent.Kirk

2 Answers

3
votes

Just specify the IndexName in addition to the TableName when you call the Query API. (Docs.) The rest is the same as if you were querying the table.

1
votes

Im having the same problem very often

what I do is , add another attribute named "all" with value of 1
(you can use any key/value )
and then create a GSI

  • partition: all
  • sort: expires

to optimise a bit you could add this attribute only to active users

or you could add an attribute active Number and use it as partition key for the GSI

this is a very inefficient partition key distribution
since all items belong to one partition but I found no other way around

I'd be happy to hear another solution