0

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?

3
  • A few questions: what exactly do you mean by avoid duplication? Does an ItemId correspond to a bunch of BatchIds? Commented Jan 14, 2015 at 2:36
  • Yes each itemID will be in multiple batches. And each batchId will have multiple itemIDs Commented Jan 14, 2015 at 18:33
  • you mean that a) a query by itemId should result in multiple item+batch elements. and b) a query by batch should result in multiple batch+item elements? (item+batch=batch+item) Commented Jan 15, 2015 at 18:21

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

I thought I saw somewhere in the documentation that to perform Query operation you must have range key (struggling to find it right now).Then wouldn't I have to duplicate the ItemId as secondary global index since I can't make the range key a global secondary index? Table1: Hash-BatchId Range-ItemId SecondaryGlobalIndex-ItemId If we can query a table that has a Hash and GlobalSecondary Index (without range key) then this solution would in fact work
This looks like a comment, rather than an attempt at answering the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.