Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Page View Counter like on StackOverFlow

Tags:

asp.net

What is the best way to implement the page view counter like the ones they have here on the site where each question has a "Views" counter?

Factoring in Performance and Scalability issues.

like image 587
TimLeung Avatar asked Oct 29 '08 14:10

TimLeung


2 Answers

The counter i optimized works like this:

UPDATE page_views SET counter = counter + 1 WHERE page_id = x
if (affected_rows == 0 ) {
   INSERT INTO page_views (page_id, counter) VALUES (x, 1)
}

This way you run 2 query for the first view, the other views require only 1 query.

like image 70
Bob Fanger Avatar answered Sep 27 '22 18:09

Bob Fanger


An efficient way may be : Store your counters in the Application object, you may persist it to file/DB periodically and on application close.

like image 44
François Avatar answered Sep 27 '22 16:09

François