2
votes

Right now, to return a sub graph from Neo4J, I use

Match(n{id:"<uuid>"}) OPTIONAL MATCH (n)-[*..25]->(m) RETURN DISTINCT *

This is very inefficient for deeply interconnected graphs (as without RETURN DISTINCT, I get 100k results as apposed to a measly under 100 results.

How do I efficiently request all sub-nodes of a node without using APOC? (AKA, make the DISTINCT redundant instead of required in the matching; AKA, visit each node only once during matching?)

1
Can you add a minimum depth greater than 1? Or is there a rule for determining (m)? No way to enforce a single visit without APOC.Dave Bennett
@DaveBennett I don't even know if their are any children (hence why I use optional match instead of just matching it). Otherwise, won't increasing the minimum bound miss some of the immediate children? I just know the sub-graph goes 0-25 nodes deep. (25 is just my arbitrary cut off)Tezra
It's just that child expansions seems so simple and basic, that I shouldn't need APOC for it.Tezra
Absolutely you would miss intermediate nodes if you set a minimum depth. I figured the optional was there for that reason, I just thought I would ask if you had considered limiting it. APOC really is the CYPHER tool for the job.Dave Bennett
@Tezra Yes, 3.2.x uses something known as a pruning var expand when using a variable length match with an upper bound and DISTINCT nodes, it should be much more efficient. Dave Bennet is correct in that prior versions would need APOC path expander procedures for much more efficient expansions.InverseFalcon

1 Answers

1
votes

Starting with Neo4j 3.2.x, (start)-[*..25]->(children) behaves as desired.

Before that, you need to use the APOC expand function apoc.path.spanningTree