I don’t know if this is allowed, but I need clarification on a solution given to a question.
What is the best way to count page views in PHP/MySQL?
I have the exact question. I just have no idea how the solution makes any sense, here is the solution:
$sample_rate = 100;
if(mt_rand(1,$sample_rate) == 1) {
$query = mysql_query(" UPDATE posts
SET views = views + {$sample_rate}
WHERE id = '{$id}' ");
// execute query, etc
}
Any help?
Here mt_rand() generate random number between 1 to 100, so probability of that number to be one is 1/100.
If this generate 1, we are increasing value of views by 100.
So, effective increase in database per view
= ( Probability of increasing view ) * (increase in database )
= (1/ 100 )* 100
= 1
So in long run it will increase database value by 1 for each view.
This is tread-off between accuracy of post and speed. As MySQL query are more time extensive than PHP rand function calls.