I have a database with millions of records. The table is structured as:
Table name : record
Filed1: Name (varchar)(Primary key)
Field2: Record(int/bigint)
example:
Name | Record
Darrin | 256
Aaron | 3
Daryl | 12
...
I need to know what position does the user with the name 'namex' in sorted records.
Currently i implement this solution:
...
$name=namex;
$query= mysqli_query($mysqli,"SELECT Name FROM record ORDER BY Record DESC");
$x=0;
$rank=0;
if ($query->num_rows > 0) {
// output data of each row
while($row = $query->fetch_assoc()) {
if($row["Name"]==$name){
$rank=$x+1;
echo "Rank : $rank<br>";
break;
}
$x++;
}
}
...
With it and 1 million records in the database, the answer comes in about 4 second.
I tried to put a table index on the field Record but have remained the same performance.
how can I reduce the execution times?