I found a better solution than solution @ 1000111.
MySQL has a custom DB type that provides better performance.
OpenGIS in MySQL is perfect for this.
Functions are given here .
An illustrative definition is given in https://stackoverflow.com/a/166269/168 .
My solution is this:
Table DB -
CREATE TABLE geoTable ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, name VARCHAR(30) NOT NULL, geoPoint POINT NOT NULL, SPATIAL INDEX(geoPoint) ) ENGINE=MyISAM; INSERT INTO geoTable (name, geoPoint) VALUES ( "A", GeomFromText('POINT(0.1 -1.01)') ), ( "B", ST_GeomFromText('POINT(56.31 2.81)') ), ( "C", ST_GeomFromText('POINT(11.1 1.176)') ), ( "ui", ST_GeomFromText('POINT(9.1 2.1)') );
SQL Query -
SELECT id, name, X(geoPoint) AS "latitude", Y(geoPoint) AS "longitude", ( GLength( LineStringFromWKB( LineString( geoPoint, GeomFromText('POINT(51.5177 -0.0968)') ) ) ) ) AS distance FROM geoTable ORDER BY distance ASC;
An example SQL Fiddle is given here .
See runtime -

For 150 entries, this is only 13 ms.
Abrar jahin
source share