优化Neo4j Cypher查询 [英] Optimize Neo4j Cypher query
问题描述
我正在做的是获取与 users个人资料 *具有特定定向关系的所有个人资料 *,如果这些用户具有替代个人资料 *获取那些 users备用个人资料 *与之有关联的人.我还需要关系的方向.
What I'm doing is to get all profiles* who has a specific directed relation to a users profile* and if those have an alternate profile* get those in case the users alternate profile* has a relation to it. I also need the direction of the relations.
我的问题是,大约有10000个节点,大约需要5秒钟才能获取数据. 我对节点和关系有自动索引.
My problem is, with about 10000 nodes it takes about 5 seconds to get data. I have auto index on nodes and relationships.
这是我的节点之间的联系方式:
This is how my nodes are related:
User-[:profile]-> ProfileA-[:related]-> ProfileB<-[?: me]-> ProfileB2<-[?: related] -ProfileA2<-[:profile] -User
User-[:profile]->ProfileA-[:related]->ProfileB<-[?:me]->ProfileB2<-[?:related]-ProfileA2<-[:profile]-User
我的查询如下:
START User=node({source})
MATCH User-[:profile]->ProfileA-[rel:related]->ProfileB
WHERE User-->ProfileA-->ProfileB
WITH ProfileA, rel, ProfileB
MATCH ProfileB<-[?:me]->ProfileB2<-[relB?:related]-ProfileA2<-[:profile]-User
WHERE relB IS NULL OR User-->ProfileA-->ProfileB<-->ProfileB2<--ProfileA2<--User
RETURN ProfileB, COLLECT(ProfileB2), rel, relB
LIMIT 25
有什么想法可以优化查询吗?
Any idea how I can optimize the query?
- 个人资料:ProfileB
- 用户个人资料:ProfileA
- 备用配置文件:ProfileB2
- 用户备用配置文件:ProfileA2
- profiles: ProfileB
- users profile: ProfileA
- alternate profile: ProfileB2
- users alternate profile: ProfileA2
推荐答案
这是我解决的方法:
START User=node({source})
MATCH User-[:profile]->ProfileA-[rel:related]->ProfileB<-[?:me]->ProfileB2-[relB?:related]-ProfileA2
WHERE relB IS NULL OR User-[:profile]->ProfileA2
RETURN ProfileB, COLLECT(ProfileB2), rel, relB
LIMIT 25
ProfileA2<-[:profile]-User
似乎会产生无限循环.
The ProfileA2<-[:profile]-User
seemed to produce an endless loop.
仍然欢迎提出建议.
这篇关于优化Neo4j Cypher查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!