So, I had a test around with a large user database table that has just over 1 million rows of data, trying to come up with some kind of βuser rankβ, but it seems very slow compared to any other way I use this data, so I was wondering if I was wrong about this or not. I pull each row of data with two columns idand pointsgrouping by points, so people with the same points will be of the same rank, and then sorted in descending order.
Here I quickly worked to check this out:
<?php
session_start();
$rank = 0;
$query = $conn->prepare("SELECT id, points FROM users GROUP BY points ORDER BY points DESC");
$query->execute();
foreach($query as $result){
$rank += 1;
if($result['id'] == $_SESSION['myid']){
echo '' . $_SESSION['myuser'] . ' is rank ' . number_format($rank) . ' globally.';
}
}
pointsand idindexed
, , "" , , , , script, :
<?php
$starttime = microtime(true);
session_start();
$rank = 0;
$query = $conn->prepare("SELECT id, points FROM users GROUP BY points ORDER BY points DESC");
$query->execute();
foreach($query as $result){
$rank += 1;
if($result['id'] == $_SESSION['myid']){
echo '' . $_SESSION['myuser'] . ' is rank ' . number_format($rank) . ' globally.';
}
}
$endtime = microtime(true);
$duration = $endtime - $starttime;
echo '<br /><br />This page took ' . $duration . ' seconds to load.';
1,9468239237 1000 .
break; , , , , .
, : - ?