0
votes

Neo4j: Finding simple path between two nodes takes a lot of time even after using upper limit (*1..4). I don't want to use allShortestPath or shortestPath because it doesnt return all the paths.

Match p=((n {Name:"Node1"}) -[*1..4]-> (m {Name:"Node2"})) return p;

Any suggestions to make it faster ?

1
Do you need all the paths or just one? In case of the latter, just use LIMIT 1 when returning.MarcoL
No i need all the possible paths otherwise i could have used shortestPathAbbas_Zaidi

1 Answers

2
votes

If you have a lot of nodes, try creating an index so that the neo4j DB engine does not have to search through every node to find the ones with the right Name property values.

I am presuming that, in your example, the n and m nodes are really the same "type" of node. If this is true, then:

  1. Add a label (I'll call it 'X') to every node (of the same type as n and m). You can use the following to add the 'X' label to node(s) represented by the variable n. You'd want to precede it with the appropriate MATCH clause:

    SET n:X

  2. Create an index on the Name property of nodes with the X label like this:

    CREATE INDEX ON :X(Name);

  3. Modify your query to:

    MATCH p=((n:X {Name:"Node1"}) -[*1..4]-> (m:X {Name:"Node2"})) RETURN p;

If you do the above, then your query should be faster.