Nickolay Kondratyev
Nickolay Kondratyev

Reputation: 5211

DynamoDB : Is it possible to query by hash and range without creating two tables?

If I want to create a DynamoDB table with ItemId and BatchId and I want to be able to query by ItemId and BatchId do I have to create two tables:

Table1: Hash-ItemId Range-BatchId
Table2: Hash-BatchId Range-ItemId

Or is there a way to use secondary indexes to avoid duplication?

Upvotes: 0

Views: 429

Answers (1)

Erben Mo
Erben Mo

Reputation: 3614

how about a global secondary index on Table1 with BatchId as hash key?

Reopen because I think this answer is useful. Please correct me instead of closing the answer. @rfornal @Devin.

Hey @Nickolay I saw your comments below. Range key of base table CAN be used as hash key of GSI.

To prove that I created a table like this:

Base table: HashKey: hash + RangeKey:range

GSI table: HashKey: range + Rangekey: hash

Inserted some keys: enter image description here

query base table: enter image description here

query gsi: enter image description here

Upvotes: 1

Related Questions