Possible Duplicate:
Mysql rank function
I am making a rank website and each user will have a unique rank based on level. #1 is the best rank and since there's 350,000 users, the worst rank will be #350000.
No user can have the same rank as anyone else. Now, when a new user is added, their level is calculated. After calculation a script will "rebuild" the ranks, by going through each user one by one, and calculating their new rank.
Here's an explanation of the queries:
- "Id" is the member's ID in the database
- "RankNum" is their level. It needs to be renamed.
- "Rank" is their unique rank, #1 to #350000.
Here's my current script:
function RebuildRanks() {
$qD = mysql_query("SELECT `Id`,`RankNum` FROM `Members` ORDER BY `Rank` DESC, `Id` ASC");
$rowsD = mysql_num_rows($qD);
$curRank = 0;
for($x = 1; $x <= $rowsD; $x++) {
$rowD = mysql_fetch_array($qD);
$curRank++;
if($rowD['RankNum'] != $curRank) {
if($curRank != 0) {
mysql_query("UPDATE `Members` SET `RankNum`='$curRank' WHERE `Id`='".$rowD['Id']."'");
}
}
}
return true;
}
With 350,000 users, this tends to run very slow. Essentially, in the database the Rank ("ORDER BY `Rank` DESC") is their level, so the query will order them. Unfortunately, the rest of the process is slow.
It takes about 97 seconds to process all 350,000 users this way. Is there any possible solution to running this more efficiently and quickly?