I have a Table foo which records the sightings of bird species. foo_id is its PK, other concerned columns are s_date, latitude and longitude. species_id is its FK. I have indexes on s_date, latitude and longitude, species_id. Table foo has 20 million records and increasing. The following query gives me top 10 latest species sightings in a given lat/long. The query is taking too much time (10+ mins sometimes). How to optimize it? I am using mysql.
SELECT species_id, max(s_date)
FROM foo
WHERE latitude >= minlat
AND latitude <= maxlat
AND longitude >= minlon
AND longitude <= max lon
GROUP BY species_id
ORDER BY MAX(s_date) DESC LIMIT 0, 10;