Cypher查询在中等大小的数据集上变得非常慢(带循环)

时间:2016-06-15 15:27:57

标签: neo4j dataset shortest-path

这个问题进一步扩展了这个问题的想法: Cypher: how to find all the chains of single nodes not repeated?

例如,在这样的图表中:

(a1:TestNode)-[:REL]->(r1:Route)-[:REL]->(a2:TestNode)-[:REL]->(s1:Route)-[:REL]->(a1:TestNode)
(a2:TestNode)-[:REL]->(r2:Route)-[:REL]->(a3:TestNode)-[:REL]->(s2:Route)-[:REL]->(a2:TestNode)
(a3:TestNode)-[:REL]->(r3:Route)-[:REL]->(a4:TestNode)-[:REL]->(s3:Route)-[:REL]->(a3:TestNode)

图形:

                     s3 ← a4
                   ↙    ↗
            s2 ← a3 → r3
          ↙    ↗
   s1 → a2 → r2
 ↙    ↗
a1 → r1

Cypher代码:

CREATE (a1:TestNode {name:'a1'})-[:REL]->(r1:Route {name:'r1'})-[:REL]->(a2:TestNode {name:'a2'})-[:REL]->(s1:Route {name:'s1'})-[:REL]->(a1),
(a2)-[:REL]->(r2:Route {name:'r2'})-[:REL]->(a3:TestNode {name:'a3'})-[:REL]->(s2:Route {name:'s2'})-[:REL]->(a2),
(a3)-[:REL]->(r3:Route {name:'r3'})-[:REL]->(a4:TestNode {name:'a4'})-[:REL]->(s3:Route {name:'s3'})-[:REL]->(a3)

之后,我们可以通过此命令找到从a4到a1的路径:

MATCH p = (a4:TestNode {name: 'a1'})-[r:REL*]->(a1:TestNode {name: 'a4'})
WITH [a4] + nodes(p) AS ns, p
    WHERE ALL (n IN ns 
        WHERE 1=SIZE(FILTER(m IN TAIL(ns) 
                            WHERE m = n)))
RETURN p

问题: 1.如果我将上述创建查询扩展为2,000' a'节点,即最多

(a2000)-[:REL]->(r2000:Route {name:'r2000'})-[:REL]->(a2001:TestNode {name:'a2001'})-[:REL]->(s2000:Route {name:'s2000'})-[:REL]->(a2000),

我发现我的电脑变得非常慢,并且neo4j占用了2GB的内存。这是正常的吗?

  1. 然后我想找一条从a2001到a1的路线。系统找不到解决方案(这显然是a2001-> a2000-> a1999 .....-> a1)。我想这是因为它们之间的循环。在上面提到的上一个问题中,查询应该避免循环,因为不允许重复。
  2. 我的目的是扩展这个想法,以便可以在连接图上识别两个位置之间的可能路线。非常感谢。

0 个答案:

没有答案