Best method for keeping website visitor count in database
-
One of our project is a content management system something like wikipedia. So I need to calculate the visitors count getting for each article. Each article in this will have a unique id. So I am looking for a best method to keep the visitor count in database. I have a master table where article details are kept. I tried adding a column named
"PageView"
which will be updated each time the page is requested. This worked well when website gets very less traffic. But shows incorrect results when it's running on heavy traffic. I guess it's a row level locking issue. Is there any other better approach which can be used to keep the accurate visitor count ? The work around which is in my mind is, putting a lock to the row when I select"PageView"
for updating. Update the count and release the lock. If this is right, will the other select requests to the same row wait until the lock get released ?All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions
-
One of our project is a content management system something like wikipedia. So I need to calculate the visitors count getting for each article. Each article in this will have a unique id. So I am looking for a best method to keep the visitor count in database. I have a master table where article details are kept. I tried adding a column named
"PageView"
which will be updated each time the page is requested. This worked well when website gets very less traffic. But shows incorrect results when it's running on heavy traffic. I guess it's a row level locking issue. Is there any other better approach which can be used to keep the accurate visitor count ? The work around which is in my mind is, putting a lock to the row when I select"PageView"
for updating. Update the count and release the lock. If this is right, will the other select requests to the same row wait until the lock get released ?All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions
N a v a n e e t h wrote:
putting a lock to the row when I select "PageView" for updating. Update the count and release the lock
Why do you select the row and lock it? why don't you just execute a simple update ? also note that in most cases, sql server will handle locks in the best way. can you post the code you use?
Hesham A. Amin My blog
-
N a v a n e e t h wrote:
putting a lock to the row when I select "PageView" for updating. Update the count and release the lock
Why do you select the row and lock it? why don't you just execute a simple update ? also note that in most cases, sql server will handle locks in the best way. can you post the code you use?
Hesham A. Amin My blog
hspc wrote:
Why do you select the row and lock it?
As you know it's a multi threading issue. Database server is multi threaded. So if two requests came at same time, the count may appear incorrect. That's what I thought of using locking.
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions
-
hspc wrote:
Why do you select the row and lock it?
As you know it's a multi threading issue. Database server is multi threaded. So if two requests came at same time, the count may appear incorrect. That's what I thought of using locking.
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions
Hi,
N a v a n e e t h wrote:
I thought of using locking.
Thats a good idea and it will work fine.......I used same concept in one of my projects.
Regards, Sandeep Kumar.V
-
hspc wrote:
Why do you select the row and lock it?
As you know it's a multi threading issue. Database server is multi threaded. So if two requests came at same time, the count may appear incorrect. That's what I thought of using locking.
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions
I really believe that you don't need to do so. this will be safe:
Update tblPages Set ViewCount = ViwCount + 1 Where PageID = @PageID
this will achieve what you need and will not require any effort on your side to manage locking.Hesham A. Amin My blog