In case you have your own slow SQL query, you can optimize it automatically here.
For the query above, the following recommendations will be helpful as part of the SQL tuning process.
You'll find 3 sections below:
SELECT
localities.code
FROM
(SELECT
glength(LineStringFromWKB(LineString(GeomFromText(astext(locality.coordinates)),
GeomFromText(astext(PointFromWKB(POINT(663.422238,
10.398996))))))) AS distance,
locality.code
FROM
locality) localities
ORDER BY
localities.distance ASC LIMIT 1