I think the reason this search takes a long time is that its using PHP (SQL instead is quicker?):
function calc_postcode_radius($distance, $postcode)
{
$postcode_count = 0;
// Increment through all postcode IDs in DB
for($i = 1; $i < 2821; $i++)
{
// Get position of users postcode
$result_1 = mysql_query("SELECT Pcode, Grid_N, Grid_E FROM postcodes WHERE Pcode='$postcode' LIMIT 1");
$row_1=mysql_fetch_array($result_1);
$gridn[0]=$row_1['Grid_N'];
$gride[0]=$row_1['Grid_E'];
$result_2 = mysql_query("SELECT Pcode, Grid_N, Grid_E FROM postcodes WHERE Postcode_ID = $i LIMIT 1");
$row_2=mysql_fetch_array($result_2);
$gridn[1]=$row_2['Grid_N'];
$gride[1]=$row_2['Grid_E'];
// TAKE GRID REFS FROM EACH OTHER.
$distance_n=$gridn[0]-$gridn[1];
$distance_e=$gride[0]-$gride[1];
// CALCULATE THE DISTANCE BETWEEN THE TWO POINTS
$hypot = sqrt(($distance_n*$distance_n)+($distance_e*$distance_e));
$text = round($hypot/1000,2);
if($text < $distance)
{
$postcode_count++;
}
}
return $postcode_count;
}
Database structure e.g:
Postcode_ID Pcode Grid_N Grid_E Latitude Longitude
1 AB10 392900 804900 57.135 -2.117
Someone please say how I could speed this up.