Query optimisation (prepare distance)