限制密码查询 [英] Limiting Cypher queries

查看:112
本文介绍了限制密码查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用具有50000个节点和200万个关系的neo4j数据库来执行密码MATCH查询,如下所示:

I am currently using a neo4j database with 50000 nodes and 2 million relationships to perform cypher MATCH queries, like the one below:

start startnode = node(42660), endnode = node(30561)
match startnode-[r*1..3]->endnode
return r;

此查询本身提供443行,但我只希望Cypher查找5个匹配项并仅返回这些匹配项.请允许我澄清一下:我不仅希望Cypher仅返回5个结果,而且还希望cypher在找到5个结果后停止查询.我不希望Cypher得到全部443个结果.

This query by itself provides 443 rows, but I only want Cypher to find 5 matches and return those only. Allow me to clarify: I do not just want Cypher to return only 5 results, I also want cypher to STOP querying once it finds 5 results. I DO NOT want Cypher to get all 443 results.

使用LIMIT子句目前可以做到这一点吗?还是LIMIT等待所有443个结果被找到,然后只返回前5个?

Is this currently possible using the LIMIT clause? Or would the LIMIT wait for all 443 results to be found, then only return the first 5?

编辑:对于这样的复杂查询,LIMIT子句会只找到前几个结果吗?

EDIT: Will the LIMIT clause find only the first few results for a complex query like this?

start graphnode = node(1), startnode = node(42660), endnode = node(30561)
match startnode<-[:CONTAINS]-graphnode-[:CONTAINS]->endnode
with startnode, endnode
match startnode-[r1*1..1]->endnode
with r1, startnode, endnode
limit 30
match startnode-[r2*2..2]->endnode
with r1, r2, startnode, endnode
limit 30
match startnode-[r3*3..3]->endnode
with r1, r2, r3, startnode, endnode
limit 30
return r1,r2,r3;

以下是查询的profile:

==> ColumnFilter(symKeys=["  UNNAMED216", "endnode", "r1", "startnode", "r2", "r3"],   returnItemNames=["r1", "r2", "r3"], _rows=30, _db_hits=0)
==> Slice(limit="Literal(30)", _rows=30, _db_hits=0)
==>   PatternMatch(g="(startnode)-['  UNNAMED216']-(endnode)", _rows=30, _db_hits=0)
==>     ColumnFilter(symKeys=["endnode", "  UNNAMED140", "r1", "startnode", "r2"], returnItemNames=["r1", "r2", "startnode", "endnode"], _rows=1, _db_hits=0)
==>       Slice(limit="Literal(30)", _rows=1, _db_hits=0)
==>         PatternMatch(g="(startnode)-['  UNNAMED140']-(endnode)", _rows=1, _db_hits=0)
==>           ColumnFilter(symKeys=["startnode", "endnode", "  UNNAMED68", "r1"], returnItemNames=["r1", "startnode", "endnode"], _rows=1, _db_hits=0)
==>             Slice(limit="Literal(30)", _rows=1, _db_hits=0)
==>               PatternMatch(g="(startnode)-['  UNNAMED68']-(endnode)", _rows=1, _db_hits=0)
==>                 NodeById(name="Literal(List(30561))", identifier="endnode", _rows=1, _db_hits=1)
==>                   NodeById(name="Literal(List(42660))", identifier="startnode", _rows=1, _db_hits=1)

推荐答案

这取决于您正在执行的操作,但是在这种情况下,如果要在return之后添加limit 5,则可以很懒惰返回并跳过其余的比赛.如果您要排序或汇总,将无法为您完成.如果您发现不是这种情况,请在github(连同您使用的版本等)上将其报告为问题.

It depends on what you're doing, but in this case, if you were to add limit 5 after return, it would be able to lazily return and skip the rest of the matches. If you were to want to sort, or aggregate, it wouldn't be able to do that for you. If you find this to not be the behavior, please report it as an issue on github (along with the version you're using, etc.)

更新新查询

start graphnode = node(1), startnode = node(42660), endnode = node(30561)
match startnode<-[:CONTAINS]-graphnode-[:CONTAINS]->endnode // do you need this, or is it always going to be true?
with startnode, endnode                                     // ditto. take it out if it doesn't need to be here.
match startnode-[r1*1..1]->endnode // this can probably be simplified to just startnode-[r1]->endnode
with r1, startnode, endnode 
limit 30 // limit to the first 30 it finds in the previous match (this should be lazy)
match startnode-[r2*2..2]->endnode // finds 2 levels deep
with r1, r2, startnode, endnode
limit 30 // limit to the first 30 it finds in the previous match (this should be lazy)
match startnode-[r3*3..3]->endnode
return r1,r2,r3 // the last with you had was extraneous, return will function the same way
limit 30; 

因此,我假设您是在问一个问题,因为此查询的速度很慢.我可能会问,为什么您要以这种方式来拆分它,而不仅仅是startnode-[r*1..3]->endnodelimit 30?您真的需要第一个匹配项吗,还是不需要检查?您可以提供PROFILE的输出吗?

So, I assume you're asking a question because this query is slow. Might I ask why you're breaking it up this way, instead of just startnode-[r*1..3]->endnode, and limit 30? Do you really need the first match/with, or is that check unnecessary? Can you provide the output of PROFILE?

这篇关于限制密码查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆