amazon-dynamodb

How to query if multiple items exist in DynamoDB?


I have a list of ids and I want to check which of them exist in my DynamoDB in an efficient way.

For example my list of ids might be:

[1, 2, 3, 4, 5]

And my DynamoDB table named Cats has the following:

{id: 1, name: 'cat1'}, {id: 3, name: 'cat3'}, {id: 5, name: 'cat5'}

I would like to know with one call that ids 1, 3, 5 are in my DynamoDB. Not sure if this is possible with 1 get request or if I need to make 5 gets.


Solution

  • I think the answer depends on how is your data structure.

    Query requests must contain the id of the entity, so there is no way to do 1 query request and fetch all three results.

    Scan requests could fetch all entries (or a filtered version of them, based on your list) but, depending on how big your table is, that request can be quite expensive from a time perspective.

    Another option might be to create a secondary index based on some other semantic your data might have and do a query/scan on that.

    Take a look at the dynamodb Best Practices page to see if you can improve your table structure to facilitate the operations.