Optimize query for calculate distance and create relationship

Solved in this post: Accessing NEAREST node - Neo4j Graph Platform / Cypher - Neo4j Online Community