1000 queries takes time(as expected), how can i approach this this in a better way

Hi everyone
I'm building an api(c#) when running this API i'm doing Neo4j Cypher queries. A worst case big query can consist of up to
1000 queries(creating a distance matrix), this will take about 10 minutes... I'm running this in individual queries(in same session). Is there a way to improve performance? Or should i tackle this from a different angle, model it differently, change my query, etc , all input appreciated :)

This is my cypher query:
MATCH (from:Location {name: 'A'})-[route1:Route]-(to:Location {name: 'B'})
RETURN route1.distanceInCm AS Route1Distance

I added an index on the 'name' property but this did not give any effect...
CREATE INDEX index_location_name FOR (n:Location) ON (n.name);

Thanks
Kim

How are you running this in C#?

You might be able to do something like

UNWIND $pairs as pair
MATCH (from: Location {name: pair.from})-[route1:Route]->(to: Location {name: pair.to})
RETURN route1.distanceInCm as Route1Distance

see: UNWIND - Cypher Manual

Are you running them in different transactions? You can run them in one transaction using a transaction function. This may help with performance.

Keep in mind, It will be an all or none rollback if an exception is thrown in any query. May also require more memory too

1 Like

Wow that is amazing from 10 min to 3 seconds! Soo happy :) You are the best thanks a bunch

Modified qyery:
WITH ['A', 'B', 'C'] AS locationIds
UNWIND locationIds AS fromId
UNWIND locationIds AS toId
MATCH (from:Location {name: fromId}), (to:Location {name: toId})
OPTIONAL MATCH (from)-[route:Route]-(to)
RETURN fromId, toId, coalesce(route.distanceInCm, -1) AS distance