简体   繁体   English

在带有密码查询语言的neo4j中,如何限制功能

[英]In neo4j with cypher query language, how does the limit functionality

While using neo4j graph database with cypher query language, I found that some computationally expensive queries run faster when "LIMIT {number}" is added to the end of the query. 当使用带有密码查询语言的neo4j图形数据库时,我发现当在查询末尾添加“ LIMIT {number}”时,某些计算量大的查询运行速度更快。 I also met few other queries in which even when added limit 1, it runs forever. 我还遇到了其他一些查询,即使添加了限制1,它也可以永远运行。 (This happened when I was executing a variable path length query ) (这是在我执行可变路径长度查询时发生的
As given in the specification, LIMIT constrains the number of rows in the output. 如规范中所给,LIMIT限制输出中的行数。 In that case my understanding is that the query is completed as such and only some rows are output. 在那种情况下,我的理解是查询就这样完成了,只输出了一些行。 In which case, all computationally expensive queries should not be affected by the limit. 在这种情况下,所有计算量大的查询都不受该限制的影响。 How exactly does LIMIT work in neo4j cypher query? LIMIT在neo4j密码查询中如何工作?

First and foremost, it is worth mentioning that Cypher is a descriptive language, meaning that you say what you want, not how to get it. 首先,值得一提的是,Cypher是一种描述性语言,意味着您说的是您想要的,而不是如何获得它。 This means that in a simple MATCH (d:Cat)-[o:OWNS]->(g:Person) , Cypher is free to start its search on d, o , or g, and then expand/filter as it likes to find all matches. 这意味着,在简单的MATCH (d:Cat)-[o:OWNS]->(g:Person) ,Cypher可以自由地在d,o或g上开始搜索,然后根据需要扩展/过滤查找所有匹配项。 This means that how a Cypher runs is not guaranteed, just the results it produces. 这意味着不能保证Cypher的运行方式,只能保证其产生的结果。 A Cyphers performance will also very based on which interpreter version you are running it on (as it may decide to do smarter/dumber things). Cyphers的性能也将非常取决于您所运行的解释器版本(因为它可能会决定执行更智能/更笨的事情)。

If you have the LIMIT keyword, but EVERYTHING must be computed to take an accurate limit, the LIMIT keyword can only help by limiting rows in future matches. 如果您使用LIMIT关键字,但必须计算所有内容以获取准确的限制,则LIMIT关键字只能通过限制将来匹配项中的行来提供帮助。 If you just need "First 5 Cats" MATCH (n:Cat) RETURN n LIMIT 5 , Than once Cypher has matched 5 cats, it knows it will just throw anything more out, so it can stop after 5 matches instead of matching all cats and taking 5. If you do MATCH (n:Cat) RETURN n ORDER BY n.name DESC LIMIT 5 , since match 5 is dependent on the sort, Cypher has no choice but to load all cats, sort them, and then limit to first 5. 如果您只需要“前5只猫” MATCH (n:Cat) RETURN n LIMIT 5 ,那么Cypher匹配5只猫之后,它知道它将抛出更多东西,因此它可以在5次匹配后停止,而不是匹配所有猫并取5。如果您进行MATCH (n:Cat) RETURN n ORDER BY n.name DESC LIMIT 5 ,则由于匹配5取决于排序,因此Cypher别无选择,只能加载所有猫,对其进行排序,然后限制为前5。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM