Search code examples
amazon-dynamodbdynamodb-queriesamazon-dynamodb-index

Dynamodb range query on timestamp


We have a DDB with a column : Timestamp (string)

Essentially we need to query data between a range of the Timestamp column.

What is the best way to do this?

I tried creating a GSI in my test environment using the Timestamp column but quickly realized that I will not be able to query a range of this column.

Edit: What I found as the best way to do this so far:

  • GSI on an event_type that we know will always be equal to Update
  • Added sort key as the Timestamp column instead, so I am able to query on a range of the timestamp

Do let me know if you know a better way to do this. Thanks.


Solution

  • Your approach is good. DynamoDB supports dates and you can do your query using "BETWEEN".

    A much better, but situational, approach is to include the time range in the partition key. If your time ranges are always of the same size for example 1 day you can do something like

    PK                             
    EVENT_TIME_RANGE#(start <-> end) 
    

    Then retrieve all of the entires in this time range with a very effective PK lookup query.

    If you can't do that but need to optimize for time range lookups you can copy the data into this "home made" time range index.