1
votes

I have Messages table in DynamoDB. It has four columns sender, timestamp, message, recipient. I was wondering instead of creating a partition key using any one of the four columns, why not create another columns for partitioning purposes concatenating sender&timestamp&recipient.

So this column will hold data like JohnSmithID1461754484307SallyMcDonaldID.

By doing this, when searching for message from a particular sender&recipient combo, I can query by just using this one column using query like (begin with & end with). And there are a few other ways of utilizing this column.

Question 1. Am I being over complicating things here by trying to use one column instead of spreading my query into a few columns?

Question 2. Is there a noticeable performance benefit by taking this direction?

Question 3. Is this design pattern only worthwhile if I eliminate column SenderId & RecipientID for data size purposes? (I need timestamp column for sort key)

2

2 Answers

4
votes

I think you have to read again how DynamoDB partition keys work. You are not able to do queries like "begin with" or "end with" on partition keys because you have to provide the full partition key for a query. You may only provide such a condition on the sort key (but note that there is a begins_with function, but no ends_with function).

Your idea might be based on using scans instead of queries but (regarding question 2.) this would result in a lot more used capacity and bad performance because DynamoDB has to take a look at every item in the table. If you want to have more query flexibility you could define one or more secondary indexes.

You can answer question 3 by yourself: DynamoDB volume is quite expensive but we are talking about a difference of maybe 20 byte per entry. If you may end up with >10.000.000 entries in your table this might become an issue, otherwise ignore it.

1
votes

Your particular example will not work, because you cannot have conditions on the Partition Key when querying. You can only have such conditions on the Sort Key.

Although, this sort of structure might come in handy at times. An example would be if you have three attributes that you want to query by. DynamoDB allows for at most two (Partition Key + Sort Key), so one of them could be a combination of two or more attributes in that case.