views:

320

answers:

3

I have created the following MySQL table to store latitude/longitude coordinates along with a name for each point:

CREATE TABLE `points` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(128) NOT NULL,
  `location` point NOT NULL,
  PRIMARY KEY (`id`),
  SPATIAL KEY `location` (`location`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

I am trying to query:

  • all points within an n mile radius of a given point;
  • the distance of each returned point from the given point

All of the examples I have found refer to using a minimum bounding rectangle (MBR) rather than a radius. The table contains approximately 1 million points, so this need needs to be as efficient as possible.

I look forward to your suggestions.

Many thanks.

A: 

Have you looked into Hilbert curves solutions?

p.marino
Thanks, I will take a look at this.
gjb
+2  A: 

Radius is not efficiently indexable. You should use the bounding rectangle to quickly get the points you are probably looking for, and then filter points outside of the radius.

Lukáš Lalinský
Thanks for you answer.Is the radius index limitation just a problem in MySQL? I'm wondering whether PostgreSQL might be more workable?How would I eliminate points from the bounding rectangle that don't lie within the radius?
gjb
No, that's a general problem. PostgreSQL does make it easier for you, because you can explicitly ask whether the point is contained in a circle and this would use the index as well as it can, but I believe it would also use only a rectangular search first. I can't see any MySQL function to do that, but you can simple calculate the distance between the center and the point.
Lukáš Lalinský
A: 

Thank you both for your answers.

I eventually found the solution at http://www.movable-type.co.uk/scripts/latlong-db.html.

gjb