Question

I'm creating a page hit counter for my busy blog.

I have a DB table called blog_article_hits, which contains three columns:

article_id    INT
hit_counter   INT
last_viewed   DateTime

Each time a visitor hits my page, my plan is to pull the current number of hits for the article, add 1 to it, and update the table again with the new value and time. I know this works, but is this the right way to accomplish this? My concern is what happens when two unique people visit the same article, at exactly the same time. Is it possible that I could lose a count? Am I supposed to be using a stored procedure or another method?

Was it helpful?

Solution

Just issue an update state directly or from a stored procedure. You will not miss any hits.

update blog_article_hits set article_id=article_id+1, last_viewed=Now()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top