0
votes

I am inheriting DynamoDB from someone.

There is a table called Item. It used an item id as a partition key which is also primary key(no sort key in this table).

Each item has a Tags attribute which is a list like tag1, tag2, etc. Now, I got a new use case that I want to query item by tags efficiently. What is the best solution to this?

I am thinking creating another table for Tags which will be a partition key and item id becomes its sort key. Is it the best solution besides re-designing Item table?

Partition key(Primary key)   tags.           name        other attributes....
 
id1                          t1,t2.          Item1Name    ... 
id2                          t1,t3,t4,t5.    Item2Name    ...
... 

My idea is to create another table, is it the best solution? any idea is appreciated.

Partition key(Primary key)   sort key     
 
t1                           id1
t1                           id2        
t2                           id1
t3                           id2
t4                           id2
t5                           id2
... 

1

1 Answers

0
votes

I think the best solution would require you to recreate the table and take the benefit of using a GSI (Global Secondary Index).

If you create the DynamoDB table to have a partition of primary key and then then a sort key of the tag then the data associated with the row you would perform a query like normal to retrieve based on your ID.

You would then create a GSI with the partition key of the tag (and perhaps sort key as the id assuming you need it) along with projecting any attributes you would want available to the GSI.

This approach is better than attempting to manage data between 2 seperate DynamoDB tables as you will only have to make the change once but can retrieve the data easily for both scenarios.