Search code examples
amazon-web-servicesnosqlamazon-dynamodb

Querying DynamoDB by date


I'm coming from a relational database background and trying to work with amazon's DynamoDB

I have a table with a hash key "DataID" and a range "CreatedAt" and a bunch of items in it.

I'm trying to get all the items that were created after a specific date and sorted by date which is pretty straightforward in a relational database.

In DynamoDB the closest thing I could find is a query and using the range key greater than filter. The only issue is that to perform a query I need a hash key which defeats the purpose.

So what am I doing wrong? Is my table schema wrong, shouldn't the hash key be unique? Or is there another way to query?


Solution

  • Updated Answer:

    DynamoDB allows for specification of secondary indexes to aid in this sort of query. Secondary indexes can either be global, meaning that the index spans the whole table across hash keys, or local meaning that the index would exist within each hash key partition, thus requiring the hash key to also be specified when making the query.

    For the use case in this question, you would want to use a global secondary index on the "CreatedAt" field.

    For more on DynamoDB secondary indexes see the secondary index documentation

    Original Answer:

    DynamoDB does not allow indexed lookups on the range key only. The hash key is required such that the service knows which partition to look in to find the data.

    You can of course perform a scan operation to filter by the date value, however this would require a full table scan, so it is not ideal.

    If you need to perform an indexed lookup of records by time across multiple primary keys, DynamoDB might not be the ideal service for you to use, or you might need to utilize a separate table (either in DynamoDB or a relational store) to store item metadata that you can perform an indexed lookup against.