Solved

searching closest postcode using latitude longitude with mysql spatial type

Posted on 2012-03-27
5
794 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

914 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

16 Experts available now in Live!

Get 1:1 Help Now