限制查询在密码查询中查询的路径数,而不是限制 [英] Limiting number of paths the query search in cypher query other than limit
问题描述
我希望查询在找到前10条路径后立即停止并返回它们. 但是默认情况下,limit子句查找所有路径,然后仅返回前10条路径. 因为在我的情况下,总路径将在1万到20万左右,所以这样做并不现实. 我尝试了以下两个不起作用的查询
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
两者都需要永远完成.
推荐答案
请确保已建立索引:
CREATE INDEX ON :Job(name)
通过在neo4j-shell中使用PROFILE
检查语句,我发现以下是最便宜的变体:
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
请注意,我说的是Neo4j 2.1.6.由于Cypher的实现正在稳步发展,因此即将发布的版本可能已经适当地优化了您的语句.
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.
这篇关于限制查询在密码查询中查询的路径数,而不是限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!