优化 Neo4j Cypher 查询 [英] Optimize Neo4j Cypher query
问题描述
我正在做的是获取所有与用户个人资料*有特定直接关系的个人资料*,如果这些个人资料有备用个人资料* 获取那些,以防 用户备用配置文件* 与之相关.我还需要关系的方向.
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:
用户-[: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
知道如何优化查询吗?
- 个人资料:个人资料B
- 用户个人资料:个人资料A
- 备用配置文件:ProfileB2
- 用户备用配置文件: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屋!