I notice a pattern which seems pretty obvious now. Need to get your opinion on this.
Suppose have a One To Many relationship from table 1 to table 2 in a relational model. For example table 1 could be a User table and table 2 could be a Login table which logs all user logins. One user can log in multiple times. Given a user we can find all logins by that user.
The first idea that comes to mind will be to store the logins only in the login table. This is design one.
But if for some usecases we are interested in a particular login of the user (say the last login) it is "generally a good idea" to cache the last login time in the user table itself. Is that right?
Design 2 is obviously redundant as we can always find the last login time by performing a join and then discarding all but the previous logins.
For one user either should be fine. But if you want to find last login time for all the users a SQL query then design 1 would involve a join and a subquery to filter out the unneeded results.
But given our usecase it is a good idea to store last login time in the user table itself which will save us from the join. Is that right?
Is that a generic pattern that you see when designing schemas?