Solved

searching closest postcode using latitude longitude with mysql spatial type

Posted on 2012-03-27
5
792 Views
Last Modified: 2012-06-21
Using mysql db. I have a column "latlong" which is a point type with a spatial index. I would like to get the closest locations 4 miles closest to the latitude and longitude.

So far I have this...

SELECT `postcode`,county, ( 3959 * acos( cos( radians(51.585738) ) * cos( radians( x(GeomFromText(astext(latlong))) ) ) * cos( radians( y(GeomFromText(astext(latlong))) ) - radians(-0.260878) ) + sin( radians(51.585738) ) * sin( radians( x(GeomFromText(astext(latlong))) ) ) ) ) AS distance  
FROM uk_p HAVING distance < 4 ORDER BY distance LIMIT 0 , 20

 I only have 2900 records, and it takes approximately 0.0277 secs. Is there anyway of optimising this query, as I am worried that as the database grows, the slower this query will be...

Or does anyone know of any other approach that will be alot quicker?
0
Comment
Question by:Rakz0r
  • 4
5 Comments
 
LVL 51

Assisted Solution

by:HainKurt
HainKurt earned 250 total points
ID: 37773214
yes you can do this...
add a where condition and add

where
X(Geomfromtext(Astext(latlong))) between 51.585738 and 51.585738+4
and
Y(Geomfromtext(Astext(latlong))) between -0.260878 and -0.260878+4

what I think is, filter the data first before calculating the distance for all points...
hope it may improve it :)
0
 

Author Comment

by:Rakz0r
ID: 37773284
I added the filter (below), and it does not bring up all the results that I expected. Anything wrong with this? Was expecting 14 records, but instead got 5 after adding this.

SELECT `postcode`,county, ( 3959 * acos( cos( radians(51.585738) ) * cos( radians( x(GeomFromText(astext(latlong))) ) ) * cos( radians( y(GeomFromText(astext(latlong))) ) - radians(-0.260878) ) + sin( radians(51.585738) ) * sin( radians( x(GeomFromText(astext(latlong))) ) ) ) ) AS distance  
FROM uk_p where X(Geomfromtext(Astext(latlong))) between 51.585738 and 51.585738+4 and Y(Geomfromtext(Astext(latlong))) between -0.260878 and -0.260878+4
 HAVING distance < 4 ORDER BY distance LIMIT 0 , 20
0
 

Author Comment

by:Rakz0r
ID: 37773319
I changed it slightly, but this only shedded off 0.0028 secs off the result. Any other ways to optimise this? Am i getting the latitude and longitude value correctly from the latlong column using x( GeomFromText( astext( latlong ))) ???

SELECT`postcode` , county, ( 3959 * acos( cos( radians( 51.585738))* cos( radians( x( GeomFromText( astext( latlong )))))* cos( radians( y( GeomFromText( astext( latlong ))))- radians(- 0.260878))+ sin( radians( 51.585738))* sin( radians( x( GeomFromText( astext( latlong )))))))AS distance
FROM uk_p
WHERE ( 3959 * acos( cos( radians( 51.585738))* cos( radians( x( GeomFromText( astext( latlong )))))* cos( radians( y( GeomFromText( astext( latlong ))))- radians(- 0.260878))+ sin( radians( 51.585738))* sin( radians( x( GeomFromText( astext( latlong )))))))<4
ORDER BY distance
0
 

Accepted Solution

by:
Rakz0r earned 0 total points
ID: 37783484
ok I have managed to optimise it further.

instead of X(Geomfromtext(Astext(latlong))) to get the lat value from the point, simply use
x(latlong).

Original query was taking 0.0251 sec but now it takes 0.0035 sec with the query below.

So now I have...

SELECT`postcode` , county, ( 3959 * acos( cos( radians( 51.585738))* cos( radians( x( latlong )))* cos( radians( y( latlong ))- radians(- 0.260878))+ sin( radians( 51.585738))* sin( radians( x( latlong )))))AS distance
FROM uk_p
WHERE ( 3959 * acos( cos( radians( 51.585738))* cos( radians( x( latlong )))* cos( radians( y( latlong ))- radians(- 0.260878))+ sin( radians( 51.585738))* sin( radians( x( latlong )))))<4
ORDER BY distance
0
 

Author Closing Comment

by:Rakz0r
ID: 37800035
my answer gave the best optimisation
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now