Niels Kristian
Niels Kristian

Reputation: 8825

How to query: filtering on multiple primary partition keys and range on primary sort key

Background I have a dynamoDB table consisting of a Primary Partition Key clientID ("N") and a Primary Sort Key createdAt("S") (as UTC ISO 8601 date) + some additional attributes.

Problem I Want to query back, all items where createdAt BETWEEN two dates and only for around 100 specific clientIDs at a time.

Upvotes: 1

Views: 5520

Answers (3)

Waleed93
Waleed93

Reputation: 1300

Or another viable solution is using PartiQL

Upvotes: 0

Niels Kristian
Niels Kristian

Reputation: 8825

I ended up doing something else:

I added a new attribute on called index_date which only holds the date part of created_at (not time). Then I added a new secondary global index on this attribute, with created_at as sort key. In this way, I can efficiently query back all items for one day (in a specific time frame if I want) at a time. Then process them afterwards.

Upvotes: 2

ketan vijayvargiya
ketan vijayvargiya

Reputation: 5649

The two options you mention, are actually the only ones you have.

  • Option 1: Use BatchGetItem for required clientID, and then do application-side filtering on createdAt.
  • Option 2: Fire 1 Query for each clientID and createdAt range, and then merge data on application-side.

You need to choose one of the two, depending on your access pattern, table structure and cost factor.

For e.g., if the average number of createdAt per clientID isn't high, Option 1 would be good enough, as it'll also be frugal on read capacity units.

EDIT: As pointed out by Niels in this answer's comment, Option 1 won't work in this particular scenario, as BatchGetItem needs both partition and sort keys.

Upvotes: 1

Related Questions