Most efficient MySQL solution for querying against coordinates
-
29-06-2021 - |
题
I have a database of geolocalized objects:
- id
- latitude
- longitude
I am querying the database to fetch objects that are inside a box:
WHERE longitude >= - 33.83789
AND longitude <= 33.837891
AND latitude >= 4.214943
AND latitude <= 40.979898
There is one index on: latitude, longitude
.
Is there a more efficient way of storing/querying the database? Right now, depending of the box coordinates/objects density, query times vary from 0.1s to more than 1s.
I know about MySQL Spacial extension, but it seems a bit of work to use and I have no idea if it will be easier/more efficient.
Additional info: I am also planning of performing simple distance-based queries (fetch objects near a specified location) in the future.
解决方案
Is there a more efficient way of storing/querying the database?
Absolulely, yes! Spatial extensions are exactly designed to solve this problem.
I know about MySQL Spacial extension, but it seems a bit of work to use and I have no idea if it will be easier/more efficient.
It's not easier (but it's not much harder), and it is more efficient. For large data sets it can be a lot more efficient.