Introduction
I am wondering what is the best way to implement a unique views system... I think I know how, so if I could explain how I think to do it and you point out any mistakes or improvements.
obviously I will have to store a log table containing a video id and something which (relatively) uniquely identifies the user. At first I considered a combination of header request and IP but decided to keep it simple and use just IP. Also that way a user can not increase the views of their video by using a different browser.
This is how I would think to do it:
When a user visits I do a SELECT similar to this:
SELECT 1 FROM tbl_log WHERE IP = $usersip AND video_id = $video_id
if there is no result then I must insert a record
INSERT into tbl_log (IP,video_id) VALUES ($usersip, $video_id)
and increase the views by 1
SELECT views FROM tbl_video WHERE video_id = $video_id
UPDATE tbl_video SET views = $result['views'] + 1 WHERE video_id = $video_id
Questions
I guess I do not want to have
millions of log records slowing down my site so should I run a cron job to empty the log table once a day?Should I make the views
transactional? (I guess a slightly
depreciated view count is less
important than a slow site because of row locks)Is there a way to reduce the load on the mysql server.... I fear if every view requires an increased view count and an IP log that it will be pretty expensive. I have seen that youtube
and the like do not update the views instantly... do they cache the
updates some how and then run them at once? if so how?How efficient is my system? Can you think of any improvements?