I have database with 1 mio records that shows user points for each project. More points user has higher is his rank.
Table user_points
user_id | points | project_id | rank
1 | 2434 | 1 | 1
2 | 43 | 2 | 3
...
Now I want to update rank every 24 hours based on users points for each project so it won't show just how many points user has but also what is his rank based on how many points other users have. To calculate users rank I made this code:
$stmt = $this->conn->prepare("SELECT * FROM user_points WHERE project_id=1");
$stmt->execute();
$users = $stmt->fetchAll();
foreach($users as $user){
$sql = "SELECT COUNT(user_id) FROM `user_points` WHERE project_id=1 AND points<".$user['points']
$stmt = $this->conn->prepare($sql);
$rank = $stmt->fetchColumn();
$sql2 = "UPDATE user_points SET rank=$rank WHERE project_id = 1 AND user_id=".$user['user_id'];
...
}
Obviously this is very slow. I tested it with wamp and 1 mio records and it updated only 300 records per minute. What technology or method I could use for such scenario?