Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jan 2007
    Posts
    217
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Optimising Queries with PHP

    Hey guys,

    I have one single update query which is run everytime a page is loaded.

    What it does is +1 to the amount of pageviews, updates the IP field to the users current IP and thats all.

    So when I have say 100 users online, each time one loads a page, this query is ran. I'm wondering if a better way to do this would be to do something like:

    $_SESSION['count']++;
    if($_SESSION['count'] == 50) {
    $u=mysql_query("UPDATE ...") or die(mysql_error());
    $_SESSION['count']=0;
    }

    so this would update every 50 pageviews, instead of every pageview.

    Or is there an even better method :P?

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    That's only going to work after one person views 50 pages during a single session lifetime. Probably not what you want.
    Are you a Help Vampire?

  • #3
    Regular Coder
    Join Date
    Jan 2007
    Posts
    217
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Cookie then?

  • #4
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Both sessions and cookies are user-specific. They can't be used (directly) to track global page loads. Your database method is probably the best option.

    By the way, for the db updates, are you using some kind of row/table locking while updating the row? If not, you may not be getting accurate results.
    Are you a Help Vampire?

  • #5
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    How about saving the last impression times of users(based on their session id) and compare this value to the time of subsequent hit? Something like
    Code:
    if(no previous impression)
      insert a row with currentTime and session (and optionally user-agent)
    else if(currentTime - impression_time > A predefined CONSTANT)
      increment the hit_count of that user
    else
      update impression_time
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #6
    Regular Coder
    Join Date
    Jan 2007
    Posts
    217
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tomws View Post
    By the way, for the db updates, are you using some kind of row/table locking while updating the row? If not, you may not be getting accurate results.
    Would this matter if its like:

    `pageviews` = `pageviews` +50

    ?

    How do you lock a row/table?

  • #7
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    The pageviews count of 50 is still restricted by those pageviews happening within one session lifetime.

    For table locking, see perhaps MySQL lock tables.
    Are you a Help Vampire?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •