GIS nearby-store distance ranking

This v4.2.5 SRID-4326 distance example uses synthetic shops: x is longitude and y latitude. Angular differences are not meters.

DROP DATABASE IF EXISTS gis_near_402;
CREATE DATABASE gis_near_402;
USE gis_near_402;
CREATE TABLE shops(id INT PRIMARY KEY, name VARCHAR(20), location POINT SRID 4326);
INSERT INTO shops VALUES (1,'origin',ST_GeomFromText('POINT(0 0)',4326)),(2,'near',ST_GeomFromText('POINT(0.001 0)',4326)),(3,'far',ST_GeomFromText('POINT(0.01 0)',4326));
SELECT id,ST_Distance(location,ST_GeomFromText('POINT(0 0)',4326)) AS distance_m FROM shops ORDER BY distance_m,id;
SELECT id FROM shops WHERE ST_Distance(location,ST_GeomFromText('POINT(0 0)',4326))<200 ORDER BY id;
DROP DATABASE gis_near_402;

Distance order is 1,2,3 and the 200-meter filter returns 1,2. Use ST_Distance’s supported geodetic path; this does not imply geodetic ST_Contains support. Test long distances, poles and the antimeridian separately rather than using planar assumptions.

Apply business filters in WHERE and a stable key for equal-distance LIMIT ordering. Establish a full distance-sort baseline before range prefilters. A degree-sized bounding box is not a uniform global radius. Record dataset size, scans, cold/warm cache and plan; do not promise spatial-index acceleration without verification.