使用Neo4j 2.1.4。我有一个在节点之间有'IS‘关系(和其他类型的关系)的图。我在图中有一些层次结构(是A关系),我需要知道一个层次的后代(是A关系),它与第二个层次的后代有着特定的已知关系。如果存在特定的已知关系,则返回第一个层次结构的后代。
输入: 'ID_parentnode_hierarchy_01‘、'ID_relationship’、'ID_parentnode_hierarchy_02‘。
输出:带有“ID_parentnode__02”的“ID_relationship”的“ID_parentnode_hierarchy_01”(是A关系)的后代。
注意:图有500.000个节点和200万个关系。
我正在使用这个密码查询,但它非常慢(aprox )。在一个4GB的RAM和3 3GHz奔腾双核64位PC)。建立更快的查询是可能的吗?
MATCH (parentnode_hierarchy_01: Node{nodeid : {ID_parentnode_hierarchy_01}})
WITH parentnode_hierarchy_01
MATCH (parentnode_hierarchy_01) <- [:REL* {reltype: {isA}}] - (descendants01: Node)
WITH descendants01
MATCH (descendants01) - [:REL {reltype: {ID_relationship}}] -> (descendants02: Node)
WITH descendants02, descendants01
MATCH (parentnode_hierarchy_02: Node {nodeid: {ID_parentnode_hierarchy_02} })
<- [:REL* {reltype: {isA}}] - (descendants02)
RETURN DISTINCT descendants01;
非常感谢。
发布于 2014-10-20 04:53:28
嗯,我可以稍微清理一下您的查询--这可能有助于我们更好地理解这些问题。我怀疑这个版本会运行得更快,但使用已清理的版本,我们可以讨论正在发生的事情:(主要是消除MATCH
/WITH
不必要的使用)
MATCH (parent:Node {nodeid: {ID_parentnode_hierarchy_01}})<-[:REL* {reltype:{isA}}]-
(descendants01:Node)-[:REL {reltype:{ID_relationship}}]->(descendants02:Node),
(parent2:Node {nodeid: {ID_parentnode_hierarchy_02}})<-[:REL* {reltype:{isA}}]-
(descendants02)
RETURN distinct descendants01;
这看起来像是在搜索两个(可能很大)树,从根开始搜索由{ID_relationship}
链接的树中的两个节点。
除非您能够提供一些查询提示,说明树中的哪个节点可能有ID_relationship
或类似的东西,否则,在最坏的情况下,您可能最终会比较这两棵树中的每两个节点。这看起来可能需要n*k时间,其中n是第一棵树中的节点数,k是第二棵树中的节点数。
下面是一些需要考虑的策略--您应该根据数据使用这些策略:
[:REL* {reltype:{isA}}]
的深度设定一个范围吗?descendants01
和descendants02
中的其他标准是什么?有什么东西可以帮助查询更有选择性,这样就不会将一个树中的每个节点与另一个树中的每个节点进行比较?您可能尝试的另一种策略是:(这可能是一个可怕的想法,但值得一试) --基本上是从一个根到另一个根,在任意数量的isa
类型的无向边缘上寻找路径。您的数据模型具有具有:REL
属性的reltype
关系。这可能是反模式,而不是reltype
属性,为什么关系类型不只是这样呢?这防止了我希望编写的查询,如下所示:
MATCH p=shortestPath((p1:Node {nodeid: {first_parent_id}})-[:isA|ID_relationship*]-(p2:Node {nodeid: {second_parent_id}}))
return p;
这将返回路径从一个“根”到另一个,通过你想要的桥。然后,您可以使用path函数提取任何您想要的节点。请注意,由于您的数据模型,当前无法进行此查询。
https://stackoverflow.com/questions/26463059
复制相似问题