What is the most optimal way to do this?
- In TV Show page I need to get all progress from all episodes in that TV Show
- In Homepage I need to get the continue_watching
- In Episode I need to get progress of the episode.
- Some TV Shows have more than 100 episodes
Ex:
USER_ID | SHOW_ID | LAST_SEEN_ID | LAST_SEEN_TIME
10 | 5 | 12 | ---
USER_ID | EPISODE_ID | PROGRESS
10 | 10 | 15
10 | 11 | 20
10 | 12 | 95
OR
USER_ID | SHOW_ID | PROGRESS | LAST_SEEN_ID | LAST_SEEN_TIME
10 | 5 | {"10":15, "11":20, "12": 95} | 12 | ---
In PosgreSQL I can get the individual progress with:
SELECT progress->'10' as progress...
I think that the best method is the first but it will create a lot of rows and this could make DB slower?
progress
only stores existing episode IDs or that the value is actually a number e.g. you can't easily prevent:{"the new one": "half way through"}
. Only when this proves to give you (performance) problems, try to come up with something better. The big question typically is: how do you want to access the data? e.g. do you need to aggregate (sum, average, ...) progress over different episodes and different users?(show_id, episode_id)
is most likely (or should be) a primary key in a table namedepisode
, so yourprogress
table should actually containshow_id, episode_id
to correctly reference an episode.(user_id, show_id, episode_id)
should be the primary key, or should(user_id, episode_id)
be the primary and(user_id, show_id)
an additional index?show_id
and bring them directly with a left join.