MYSQL Geo Search having distance performance

时光总嘲笑我的痴心妄想 提交于 2019-11-28 20:56:04

The fastest way to do this is to use the geospatial extensions for MySQL, which should be easy enough as you are already using a MyISAM table. The documentation for these extensions can be found here: http://dev.mysql.com/doc/refman/5.6/en/spatial-extensions.html

Add a new column with a POINT datatype:

ALTER TABLE `adverts` 
ADD COLUMN `geopoint` POINT NOT NULL AFTER `longitude`
ADD SPATIAL KEY `geopoint` (`geopoint`)

You can then populate this column from your existing latitude and longitude fields:

UPDATE `adverts` 
SET `geopoint` = GeomFromText(CONCAT('POINT(',`latitude`,' ',`longitude`,')'));

The next step is to create a bounding box based on the input latitude and longitude that will be used in your WHERE clause as a CONTAINS constraint. You will need to determine a set of X,Y POINT coordinates that work for your requirements based on the desired search area and given starting point.

Your final query will search for all POINT data that is within your search POLYGON, and you can then use a distance calculation to further refine and sort your data:

SELECT a.*, 
    ROUND( SQRT( ( ( (adverts.latitude - '53.410778') * (adverts.latitude - '53.410778') ) * 69.1 * 69.1 ) + ( (adverts.longitude - '-2.97784') * (adverts.longitude - '-2.97784') * 53 * 53 ) ), 1 ) AS distance
FROM adverts a
WHERE a.type_id = 3
AND CONTAINS(a.geopoint, GeomFromText('Polygon((0 0,0 3,3 3,3 0,0 0))'))
HAVING distance < 25
ORDER BY distance DESC
LIMIT 0, 30

Note that the GeomFromText('Polygon((0 0,0 3,3 3,3 0,0 0))') in the above will not work, you will need to replace the coordinates with valid points around your search start. If you expect the lat/long to change, you should consider using a trigger to keep the POINT data and associated SPATIAL KEY up to date. With large datasets you should see vastly improved performance over calculating a distance for every record and filtering using a HAVING clause. I personally defined functions for use in determining the distance and creating the bounding POLYGON.

There a few ways to speed up your query, personally I'd take advantage of the POW function.

Returns the value of X raised to the power of Y.

Manual multiplication will slow your query down with large tables, although achieving the same result.

SELECT a .* , 
    round( sqrt( 
        (POW( a.latitude -'53.410778', 2)* 68.1 * 68.1) + 
        (POW(a.latitude -'-2.97784', 2) * 53.1 * 53.1) 
     )) AS distance
 FROM adverts a
     WHERE a.type_id = 3
     HAVING distance < 25
     LIMIT 0 , 30

The above query runs in 0.0008 sec on table schema with 10,000 records (Your query tested on the same table schema took 0.0129 sec), so it was a considerable increase in performance.

Other Optimization Tips

  • An sql query becomes faster if you use the actual columns names in SELECT statement instead of *.
  • Fully reference the table name mydatabase.mytable.
  • If you have to ORDER BY use the primary key (Its an indexed field, or create an index on the field you intend on ORDERING).
  • Use mysql framework functions for math calculations it will speed up the process.
  • And finally try and make your queries as simple as possible with these steps (the simpler the faster).

Sources

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!