In the previous module, you added an inverted index to your table. The inverted index adds additional query patterns for relational data. However, we saw that we still had a problem. While we could query all Friendship entities for a given User entity, the Friendship entity doesn’t contain information about the user being followed.
In this module, you will learn about partial normalization.
In the second module, we saw that you should not try to fake a relational pattern when modeling with DynamoDB. One core tenant of relational model is normalization of data in which you avoid duplicating data in multiple places. Normalization is a nice pattern in relational databases, but you may require costly joins to reassemble your data when querying.
With DynamoDB, you often want to denormalize your data. Denormalization helps to avoid joins and improve query performance. To do this, you may copy attributes from one item into another item that refers to it in order to avoid fetching both items during a query.
However, there are times when denormalization can complicate your data model. For example, the data model has a Friendship entity that refers to both a user that is being followed and a user that is following. You could copy all of the attributes of each User entity into the Friendship entity when you create the Friendship entity. Then, when you retrieve the Friendship entity, you would also have all details about both users as well.
This could pose a problem whenever a user changes information in their profile. For example, if a user changes their profile picture, the data in each Friendship entity containing that user would be stale. You would need to update each Friendship entity that contained the user whenever there is an update.
In steps below, you will see how to use partial normalization and the BatchGetItem API call to handle this situation.
Time to Complete Module: 20 Minutes