Querying distances effectively