I have a website with a user rating as the central part, but the number of users has grown to more than 50,000, and it puts a strain on the server to sort through all those that update the rating every 5 minutes. Is there a better way that you can use to quickly update ranks at least every 5 minutes? It doesnβt have to be with php, it could be something that runs as a perl script or something in this case if something like this can improve the work (although I'm not sure why this would be just leaving my options open here).
This is what I am doing now to update ranks:
$get_users = mysql_query("SELECT id FROM users WHERE status = '1' ORDER BY month_score DESC");
$i=0;
while ($a = mysql_fetch_array($get_users)) {
$i++;
mysql_query("UPDATE users SET month_rank = '$i' WHERE id = '$a[id]'");
}
UPDATE (decision):
Here is a solution code that takes less than 1/2 second to execute and update all 50,000 rows (make ranking the primary key, as Tom Haig suggested).
mysql_query("TRUNCATE TABLE userRanks");
mysql_query("INSERT INTO userRanks (userid) SELECT id FROM users WHERE status = '1' ORDER BY month_score DESC");
mysql_query("UPDATE users, userRanks SET users.month_rank = userRanks.rank WHERE users.id = userRanks.id");
source
share