MySQL – Geo-Localized Database Using RTREE

MySQLspatial

I'm using MYSQL V5.6.24 GPL

I have a database with 200K rows and growing and I'm trying to get the nearest, based on points. I have all the database in InooDB, and to use Spatial data, I create one table with the Points with Mysiam.

everything works great, the problem is that when I sort based on ST_Distance, I'm not using any index, I create a SPATIAL index with the coordinates, and still using file_sort to sort. And Mysql doesn't allow me to use RTREE INDEX

Any idea?

Best Answer

This blog explains how to lay out your data to get very good speed for "finding the nearest 10 pizza parlors" from latitude/longitude. It includes reference code. It does not depend on SPATIAL or 5.7. "200K and growing" is no problem.

It does depend on InnoDB (for clustered PRIMARY KEY), PARTITIONs (to get a pseudo-2D index), and a Stored Procedure (because of how messy the code is). And it requires using a different datatype for latitude and longitude (because of PARTITION restrictions).

(Yeah, yeah, I know you would like me to spell it out here. But it is too involved to fit here.)