Maulik Soneji
Maulik Soneji

Reputation: 177

One to Many Schema in DynamoDB

I am currently designing dynamo DB schemas for the following use case:

A company can have many channels, and then for each channel, I have multiple channel cards.

I am thinking of having following tables:

Channel Table:

Partition Key: CompanyId
Sort Key: Combination of Time Stamp and deleted or not

Now after getting the channels for a company, I need to fetch its channel cards, for this, I am thinking to have following Table Schema for ChannelCard.

ChannelCard Table:

Partition Key: channelId
Sort Key: Combination of Time Stamp and deleted or not

Now to get the channel cards for a company, I need to do the following:

 1. First query the channels for the company using partition key (1 query)
 2. Getting channel cards for each channel (number of channels query)

So in this case, we will be making many queries, can we have any less number of queries in our case?

Any suggestions for modifying the database tables or about how to query the database are welcome.

Upvotes: 1

Views: 83

Answers (1)

cementblocks
cementblocks

Reputation: 4596

You could also have

Channel Table

Partition Key: CompanyId
Sort Key: Deleted+timestamp

Channel Card Table

Partition Key: CompanyId
Sort Key: Deleted+ChannelCardTimeStamp

GSI #1:
Partition Key: ChannelId
Sort Key: Deleted+ChannelCardTimeStamp

This way you can have one query for the most recent channelcards for any given company and you can also query for the most recent channelcards for any channel.

Upvotes: 1

Related Questions