I am working on a video site. It’s pretty heavy so far as traffic is concerned. I am trying to rank videos by the number of times videos are viewed. I.e. video A gets 5.2K views and B gets 5k views, so A ranks above B. But I want to calculate the views each video gets in a week (or a month, if needed) and then rank them by view counts.
I first created a table that contains views only. i.e. every time, a video is viewed a row is inserted into the table along with timestamp and video id. That was a horrible decision. Since the traffic was heavy, the table became huge, affecting performance. Is there is a better way to do this?
I would have the table storing the total number of views for a particular video against the week number. Just create/increment as needed.
i.e. have fields video_id, week_number, and total_views.
Have a good index (i.e. on week_number, total_views) so that the top ten is easy to extract.
Also have a
cronjob to delete old data (or archive it). That should keep the tables size manageable.