user1099123
user1099123

Reputation: 6683

Why does this neo4j query not finish?

The following doesn't finish

MATCH path = (start:A)-[*]->(end:A)
WHERE start.fen = 'abc'
WITH distinct path as distinct_path
WHERE length(distinct_path) > 3
return distinct_path
LIMIT 10

But when removing the WHERE clause, it returns

MATCH path = (start:A)-[*]->(end:A)
WHERE start.fen = 'abc'
WITH distinct path as distinct_path
return distinct_path
LIMIT 10

What about the WHERE clause makes it not finish?

Upvotes: 1

Views: 30

Answers (2)

Finbar Good
Finbar Good

Reputation: 1426

The answer by Stewart correctly answers your question as to why the WHERE changes the performance.

But to actually improve the performance of your first query, move the criteria into the lower bound for the length of paths matched:

MATCH path = (start:A)-[*4..]->(end:A)
WHERE start.fen = 'abc'
WITH DISTINCT path AS distinct_path
RETURN distinct_path
LIMIT 10

Upvotes: 0

Stewart
Stewart

Reputation: 18313

Both queries have a LIMIT 10, but the second query is not qualified by any WHERE clause, so it can simply return you the first 10 it finds.

It just finds 10 and stops.

The first query, with the additional WHERE, means your returned 10 must have a path length > 3, which means it must traverse all the shorter paths and discard them, keeping track in memory of the paths it has tried. You are only looking for the longer paths. So it is simply more computation effort. And maybe more computation effort than we have patience for waiting.

Upvotes: 0

Related Questions