I currently use the following commands to compare long and lat values in two tables and update the sitecode column in one table based on the other like so:
CREATE FUNCTION great_circle_km (lon1 DOUBLE, lat1 DOUBLE, lon2 DOUBLE, lat2 DOUBLE)
RETURNS DOUBLE
RETURN 6371 * acos(cos(radians(lat1)) * cos(radians(lat2)) * cos(radians(lon2) - radians(lon1)) + sin(radians(lat1)) * sin(radians(lat2)))
UPDATE `source` SET `source`.`sitecode` = (
SELECT `sitecode` FROM `siteinfo`
ORDER BY great_circle_km(`source`.`longitude`, `source`.`latitude`, `siteinfo`.`longitude`, `siteinfo`.`latitude`)
LIMIT 1
)
However, for a reasonably large table size of 8000 rows, the MySQL takes an upwards of 2 minutes to process and complete the UPDATE command. Is there any way i can optimise this?