Limiting number of paths the query search in cypher query other than limit

霸气de小男生 提交于 2019-12-11 10:27:09

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!