# Nearest Neighbor Search by Distance in Large Datasets with MySQL

Nearest neighbor search is common in geographical databases. Here we consider searching for nearby locations in a large geographical database. Many commercial databases have geographical indexes supporting such queries; however, in MySQL there is no simple implementation. We show a simple way below.

Consider the following table definition.

```CREATE TABLE `locations` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`lat` DOUBLE NOT NULL,
`lng` DOUBLE NOT NULL,
KEY `lat` (`lat`),
KEY `lng` (`lng`)
);
```

Suppose we have a center at (\$lat0, \$lng0), and we wish to find nearby locations by distance. The following query contains a formula calculating the distance from the center. It returns the closet 20 locations within 10 km from the center. Here 6371 is the earthâ€™s radius in km.

```SELECT *,
( 6371 * acos(
) ) AS distance
FROM `locations`
HAVING distance &lt; 10
ORDER BY distance LIMIT 20;
```

The query works well on small datasets; however, it is not efficient when the table is large (say, with 1 million records). This query scans all rows in the table, calculates the distance to the center, and finally sorts the result by distance. It runs in linear time of the number of rows. None of the indexes is effective.

A quick fix is to specify a bounding box for latitude and longitude. The following query specifies a bounding box by simply adding/subtracting 1 degree from latitude/longitude of the center.

```SELECT *
FROM `locations`
WHERE lat &lt; \$lat0 + 1 AND lat &gt; \$lat0 - 1 AND
lng &lt; \$lng0 + 1 AND lng &gt; \$lng0 - 1;
```

And the next query sorts the result by the distance to the center.

```SELECT *,
( 6371 * acos(
) ) AS distance
FROM `locations`
WHERE lat &lt; \$lat0 + 1 AND lat &gt; \$lat0 - 1 AND
lng &lt; \$lng0 + 1 AND lng &gt; \$lng0 - 1
HAVING distance &lt; 10
ORDER BY distance LIMIT 20;
```

However, this simple “degree manipulation” may not be good for applications requiring precise results. A better (but more complicated) way is to calculate the extremal degrees of points within certain distance from the center (the north/south/east/west point of 10 km to the center). Suppose we have parameters (\$lat0, \$lng0, \$dist) for the center and the query distance.

```SELECT *
FROM `locations`
WHERE lat &lt; degrees( asin( sin(radians(\$lat0)) * cos(\$dist / 6371) +              cos(radians(\$lat0)) * sin(\$dist / 6371) * cos(radians(0)) ))   AND lat &gt; degrees( asin( sin(radians(\$lat0)) * cos(\$dist / 6371) +
```

The queries with bounding boxes will benefit from the indexes on “lat” and “lng”; the queries will be run with index scan on “lat” and “lng”. Note again that, we assume the dataset is large and scattered while the querying box is relatively small.

The final query below searches the nearest 20 locations within distance \$dist (in kilometers) from the center (\$lat0, \$lng0), with the result sorted by the distance.

```SELECT *,
( 6371 * acos(
) ) AS distance
FROM `locations`
WHERE lat &lt; degrees( asin( sin(radians(\$lat0)) * cos(\$dist / 6371) +
AND lat &gt; degrees( asin( sin(radians(\$lat0)) * cos(\$dist / 6371) +