0
votes

I have my dynamo db table as follows:

HashKey(Date) ,RangeKey(timestamp)

DB stores the data of each day(hash key) and time stamp(range key).

Now I want to query data of last 7 days. Can i do this in one query? or do i need to call dbb 7 times for each day? order of the data does not matter So, can some one suggest an efficient query to do that.

1
Call the query 7 times, or create new GSI with Date is RangeKey - hoangdv

1 Answers

0
votes

I think you have a few options here.

  1. BatchGetItem - The BatchGetItem operation returns the attributes of one or more items from one or more tables. You identify requested items by primary key. You could specify all 7 primary keys and fire off a single request.
  2. 7 calls to DynamoDB. Not ideal, but it'd get the job done.
  3. Introduce a global secondary index that projects your data into the shape your application needs. For example, you could introduce an attribute that represents an entire week by using a truncated timestamp:
  2021-02-08 (represents the week of 02/08/21T00:00:00 - 02/14/21T12:59:59)
  2021-02-16 (represents the week of 02/15/21T00:00:00 - 02/22/21T12:59:59)

I call this a "truncated timestamp" because I am effectively ignoring the HH:MM:SS portion of the timestamp. When you create a new item in DDB, you could introduce a truncated timestamp that represents the week it was inserted. Therefore, all items inserted in the same week will show up in the same item collection in your GSI.

  1. Depending on the volume of data you're dealing with, you might also consider separate tables to segregate ranges of data. AWS has an article describing this pattern.