문제

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?

도움이 되었습니까?

해결책

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()
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top