I use MySQL database with innoDB storage engine. I was trying to speed up the query for searching places by latitude and longitude and I found out that a search with smaller radius, less results performs slower than the one with more results. (latitude and longitude is the same, column ID is the primary key).
0.0011 sec, 15404 results
SELECT ID, 69.0 * DEGREES(ACOS(COS(RADIANS(51.5099983215332)) *
COS(RADIANS(Lat)) * COS(RADIANS(-0.05000000074505806) - RADIANS(Lng)) +
SIN(RADIANS(51.5099983215332)) * SIN(RADIANS(Lat)))) AS distance
FROM Table
WHERE (Lat BETWEEN 51.5099983215332 - (18 / 69.0)
AND 51.5099983215332 + (18 / 69.0)
AND Lng BETWEEN -0.05000000074505806 - (18 / (69.0 * COS(RADIANS(51.5099983215332))))
AND -0.05000000074505806 + (18 / (69.0 * COS(RADIANS(51.5099983215332)))) )
HAVING distance <= 18
0.0319 sec, 105 results
SELECT ID, 69.0 * DEGREES(ACOS(COS(RADIANS(51.5099983215332)) *
COS(RADIANS(Lat)) * COS(RADIANS(-0.05000000074505806) - RADIANS(Lng)) +
SIN(RADIANS(51.5099983215332)) * SIN(RADIANS(Lat)))) AS distance
FROM Table
WHERE (Lat BETWEEN 51.5099983215332 - (0.5 / 69.0)
AND 51.5099983215332 + (0.5 / 69.0)
AND Lng BETWEEN -0.05000000074505806 - (0.5 / (69.0 * COS(RADIANS(51.5099983215332))))
AND -0.05000000074505806 + (0.5 / (69.0 * COS(RADIANS(51.5099983215332)))) )
HAVING distance <= 0.5
Is that OK?
WHERE 69...would be faster.