user1584120
user1584120

Reputation: 1261

dynamodb query to select all items that match a set of values

In a dynamo table I would like to query by selecting all items where an attributes value matches one of a set of values. For example my table has a current_status attribute so I would like all items that either have a 'NEW' or 'ASSIGNED' value. If I apply a GSI to the current_status attribute it looks like I have to do this in two queries? Or instead do a scan?

Upvotes: 0

Views: 1922

Answers (1)

Sumeet P
Sumeet P

Reputation: 154

DynamoDB does not recommend using scan. Use it only when there is no other option and you have fairly small amount of data.

You need use GSIs here. Putting current_status in PK of GSI would result in hot partition issue.

The right solution is to put random number in PK of GSI, ranging from 0..N, where N is number of partitions. And put the status in SK of GSI, along with timestamp or some unique information to keep PK-SK pair unique. So when you want to query based on current_status, execute N queries in parallel with PK ranging from 0..N and SK begins_with current_status. N should be decided based on amount of data you have. If the data on each row is less than 4kb, then this parallel query operation would consume N read units without hot partition issue. Below link provides the details information on this

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-indexes-gsi-sharding.html https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-modeling-nosql-B.html

Upvotes: 0

Related Questions