问题
I want the query to stop as soon as it finds first 10 paths and return those. But by default the limit clause finds all the paths and then just returns first 10 paths. Because total paths in my case will be around 10k to 20k, its not practical to do that. i tried following two queries which dont work
match path = (first:Job)-[:PRECEDES*]->(last:Job)
where first.name = 'xyz' and last.name = 'abc'
return nodes(path) as pathlist
match path1 = (first:Job)-[:PRECEDES*]->(middle:Job)
where first.name = 'xyz'
with middle, path1
match path2 = (middle:Job)-[:PRECEDES*]->(last:Job)
last.name = 'abc'
return nodes(path1),nodes(path2) as pathlist
both are taking forever to complete.
回答1:
Be sure to have an index in place:
CREATE INDEX ON :Job(name)
By inspecting the statements using PROFILE
in neo4j-shell I've found the following being the cheapest variant:
MATCH (a:Job {name:'xyz'}), (b:Job {name:'abc'})
MATCH path=(a)-[:PRECEDES*]->(b)
RETURN nodes(path) LIMT 10
Please note that I'm talking of Neo4j 2.1.6. Since Cypher's implementation is steadily evolving, a upcoming version might already optimize your statements appropriately.
来源:https://stackoverflow.com/questions/27183614/limiting-number-of-paths-the-query-search-in-cypher-query-other-than-limit