Say I have a feature in a web app where users can create a post and like it. In the frontend the user should see the number of likes a post has.
I could store the data two ways:
1. Option 1: A small table where # of likes is determined from a query
(content: text, liked_by: string[])
2. Option 2: a larger table where # of likes is explicitly tracked
(content: text, likes: int, liked_by: string[])
Using option 1, for each Post that is shown to the user, we must retrieve an arbitrarily large array from the database (liked_by
), and then Count(liked_by)
each item in that array, which could be essentially an O(N * number_of_posts) length operation.
In option 2, we can immediately access the # of likes a post has, so this is much faster. However, now there is some redundancy or potentially unnecessary complexity in the database.
Is there a preferred pattern or method for structuring a database for this use case?