...

View Full Version : Optimising Queries with PHP



ole90
11-05-2009, 06:33 PM
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?

tomws
11-05-2009, 07:38 PM
That's only going to work after one person views 50 pages during a single session lifetime. Probably not what you want.

ole90
11-06-2009, 02:36 PM
Cookie then?

tomws
11-06-2009, 03:33 PM
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.

abduraooft
11-06-2009, 03:50 PM
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

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

ole90
11-06-2009, 05:39 PM
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?

tomws
11-08-2009, 02:50 AM
The pageviews count of 50 is still restricted by those pageviews happening within one session lifetime.

For table locking, see perhaps MySQL lock tables (http://dev.mysql.com/doc/refman/5.0/en/lock-tables.html).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum