Efficient Distance Querying in MySQL
aaronfrancis.com
aaronfrancis.com
https://dev.mysql.com/doc/refman/8.0/en/spatial-types.html
We discovered that spacial index (flatten to 1 dimension) can be used to solve a common issue: ip range queries.
The query costs dropped and all the query metrics improved.
I'll need to so similar work soon.
> it gives you results that are 100% correct, which is pretty important!
Not quite 100% correct, as the `ST_Distance_Sphere` function uses a spherical earth model, & in fact the earth is an oblate spheroid :) The slower-but-more-accurate `ST_Distance_Spheroid` also exists if you care about such things.
(if my memory serves)
At that time MySQL had neither spatial indices nor a native haversine method. I switched out the search method to translate the area name to a lat-long on the frontend (based on google maps) and then use a haversine in the query to retrieve results. It was still a couple of orders of magnitude faster than the previous solution, and more accurate to boot.
[1] TBH the first thing to look at is whether this is a real constraint