我刚开始使用 Neo4j 并尝试为大量数据创建关系。
我已经添加了一些关系,现在在尝试创建关系时会看到以下错误。
ServiceUnavailable WebSocket connection failure. Due to security constraints in your web browser, the reason for the failure is not available to this Neo4j Driver. Please use your browsers development console to determine the root cause of the failure. Common reasons include the database being unavailable, using the wrong connection URL or temporary network problems. If you have enabled encryption, ensure your browser is configured to trust the certificate Neo4j is configured to use. WebSocket
readyState
is: 3
这是我要运行的查询
MATCH (p:students),(c:results)
WHERE p.id = c.student_id
create (c)-[:scored]->(p)
学生和考试各有大约 300000 个节点。
只有在建立关系时才会发生这种情况。
但是当我为同一个匹配运行返回查询时它并没有发生
MATCH (p:students),(c:results)
WHERE p.id = c.student_id
return c.student_id
我尝试检查日志但找不到任何解决方案。有人可以帮助我了解错误的含义以及如果有人遇到类似问题如何解决。
注意:我在带有远程主机的 Ubuntu 机器上运行它。
最佳答案
一次创建 300000 个关系可能是一项昂贵的操作,主要是在堆使用方面,因为您在单个事务中执行此操作。
更好的方法是批量插入(通常每笔交易 10k 是最佳选择)。 APOC Procedures是解决此问题的首选解决方案,尤其是 apoc.periodic.iterate()
.
CALL apoc.periodic.iterate(
'MATCH (p:students),(c:results)
WHERE p.id = c.student_id
RETURN p, c',
'CREATE (c)-[:scored]->(p)',
{iterateList:true, parallel:false}) YIELD batches, total, errorMessages
RETURN batches, total, errorMessages
要检查的另一件大事是您在 :students(id) 和 :results(student_id) 上都有索引(或唯一约束),因此您的查找速度很快,否则定期迭代将挂起,因为这将成为高成本操作。
对原始查询运行 EXPLAIN 并确保它首先对其中一个节点使用 NodeByLabelScan,然后使用 NodeByIndexSeek 查找其他节点。
关于sql - 在 Neo4j 中创建节点之间的关系时 ServiceUnavailable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49629943/