1

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?

Mack
  • 117
  • 2
  • 9

1 Answers1

0

Apologies if I've missed something here but that ORDER BY looks expensive, if both tables share a common key you could you try a MIN (or MAX) and benchmark it to see if query times improve? If there is no shared key, does siteinfo contain only one row?

UPDATE `source` A
SET A.`sitecode` = (SELECT MIN(great_circle_km(A.`longitude`, A.`latitude`, B.`longitude`, B.`latitude`)) FROM `siteinfo` B WHERE A.someID = B.someID);

Please let me know if there could be a better answer if more detail is available in the light of this suggestion,

Regards,

James

James Scott
  • 1,032
  • 1
  • 10
  • 17