Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Improve reverse geocode query #5

Merged
merged 1 commit into from

2 participants

@ivebeenlinuxed
Collaborator

After a few days running, it seems that the MySQL query used for latlng to postcode needed some revision work.

Before this commit the logs shows that most of the MySQL time was spent ordering the results and doing the maths.

This fix adds a "WHERE" clause, denoting an approximate 20 mile range to "short list" the geolocation. By doing this the application is only searching a limited number of postcodes in depth, rather than performing mathematics and ordering on the entire table.

See the improvements (home PC, hense the slow file_get_contents on HTTP request) here: http://twitpic.com/bajqto/full

This fix speeds up requests from approximately 10 seconds to 0.3 seconds.

@pezholio pezholio merged commit 732e115 into pezholio:master
@pezholio
Owner

Thanks Will, that's awesome! :+1:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 2 additions and 2 deletions.
  1. +2 −2 postcode.php
View
4 postcode.php
@@ -45,8 +45,8 @@
} else {
$lat = mysql_real_escape_string($_GET['lat']);
$lng = mysql_real_escape_string($_GET['lng']);
- //$result = mysql_query("SELECT postcode, ( 3959 * acos( cos( radians($lat) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians($lng) ) + sin( radians($lat) ) * sin( radians( lat ) ) ) ) AS distance FROM postcodes WHERE ".($lat-0.1)." < lat AND ".($lat+0.1)." > lat AND ".($lng-0.1)." < lng AND ".($lng+0.1)." > lng ORDER BY distance LIMIT 0,1");
- $result = mysql_query("SELECT postcode, ( 3959 * acos( cos( radians($lat) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians($lng) ) + sin( radians($lat) ) * sin( radians( lat ) ) ) ) AS distance FROM postcodes ORDER BY distance LIMIT 0,1");
+ $result = mysql_query("SELECT postcode, ( 3959 * acos( cos( radians($lat) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians($lng) ) + sin( radians($lat) ) * sin( radians( lat ) ) ) ) AS distance FROM postcodes WHERE ".($lat-0.1)." < lat AND ".($lat+0.1)." > lat AND ".($lng-0.1)." < lng AND ".($lng+0.1)." > lng ORDER BY distance LIMIT 0,1");
+ //$result = mysql_query("SELECT postcode, ( 3959 * acos( cos( radians($lat) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians($lng) ) + sin( radians($lat) ) * sin( radians( lat ) ) ) ) AS distance FROM postcodes ORDER BY distance LIMIT 0,1");
$row = mysql_fetch_array($result);
$query = "SELECT postcode, lat, lng, easting, northing, postcodes.county AS countygss, counties.county AS countyname, counties.snacid AS countysnac, postcodes.council AS councilgss, councils.snacid AS councilsnac, councils.council AS councilname, postcodes.ward AS wardgss, wards.ward as wardname, wards.snacid as wardsnac, postcodes.constituency AS constituencygss, constituencies.constituency AS constituencyname, constituencies.snacid AS constituencycode, postcodes.parish AS parishgss, parishes.snacid AS parishsnac, parishes.parish AS parishname FROM postcodes LEFT JOIN councils ON postcodes.council = councils.code LEFT JOIN wards ON postcodes.ward = wards.code LEFT JOIN counties ON postcodes.county = counties.code LEFT JOIN constituencies ON postcodes.constituency = constituencies.code LEFT JOIN parishes ON postcodes.parish = parishes.code WHERE postcodes.postcode = '$row[postcode]' LIMIT 0,1";
Something went wrong with that request. Please try again.