The solution to this is to take into account the curvature of the earth when sorting the devices by distance.
CREATE FUNCTION `ST_DISTANCE_SPHERE`(`pt1` POINT, `pt2` POINT) RETURNS decimal(10,2)
BEGIN
return 6371000 * 2 * ASIN(SQRT(
POWER(SIN((ST_Y(pt2) - ST_Y(pt1)) * pi()/180 / 2),
2) + COS(ST_Y(pt1) * pi()/180 ) * COS(ST_Y(pt2) *
pi()/180) * POWER(SIN((ST_X(pt2) - ST_X(pt1)) *
pi()/180 / 2), 2) ));
END
//
DELIMITER ;
If we can add the above stored function to the database, we'll be able to swap out the ST_DISTANCE()
call for ST_DISTANCE_SPHERE()
instead.