首页 > 解决方案 > Neo4j python驱动程序-遍历结果非常慢

问题描述

我正在尝试从 neo4j 中获取一百万个关系以在 python 中进行一些分析,但是迭代结果需要大量时间。

下面是一个非常简化的查询,为您提供示例。我花时间遍历 250.000 条记录,结果大约是 1 分钟。我尝试只返回我想要的属性,检查数据量是否有影响,但结果几乎相同。

正常吗?我没有考虑一些重要的事情吗?我怎样才能更快地做到这一点?

提前致谢

from neo4j import GraphDatabase
driver = GraphDatabase.driver(url, auth=(user, password))

query = """
        MATCH (u:User)-[r:was_at]->(p:Place)
        RETURN u, p, r
        LIMIT 250000
    """

session = driver.session()
result = session.run(query)

i = 0
for record in result: 
    i += 1
CPU times: user 57.2 s, sys: 736 ms, total: 58 s
Wall time: 1min 7s

编辑:

请注意,在不执行任何操作的情况下迭代结果需要一分钟:

i = 0
for record in result: 
    i += 1

这正常吗?

对评论的回答:

是的,was_at关系类型只存在于 User 和 Place 节点之间。

目标是创建网络的 Igraph 实例。每个节点都有一些属性,您可以在以下示例中看到它们:

<Record 
  u=<Node id=0 labels={'User'} properties={'followers': 274, 'screen_name': 'PASTORMAURICIOR', 'name': 'PASTORMAURICIOROJAS', 'verified': 'False', 'statuses': 2434, 'created_at': 'Sat May 28 00:03:02 +0000 2011', 'id': '306492469', 'friends': 330}> 
  p=<Node id=982894 labels={'Place'} properties={'id': '8866e09315fffff'}> 
  r=<Relationship id=1556326 nodes=(
     <Node id=0 labels={'User'} properties={'followers': 274, 'screen_name': 'PASTORMAURICIOR', 'name': 'PASTORMAURICIOROJAS', 'verified': 'False', 'statuses': 2434, 'created_at': 'Sat May 28 00:03:02 +0000 2011', 'id': '306492469', 'friends': 330}>, 
     <Node id=982894 labels={'Place'} properties={'id': '8866e09315fffff'}>
    ) type='was_at' properties={'date': neotime.DateTime(2017, 1, 13, 0, 0, 0.0, tzinfo=<UTC>)}>>

当我只返回关系时,节点不包含所需的属性:

<Record r=<Relationship id=1556326 nodes=(
  <Node id=0 labels=set() properties={}>, 
  <Node id=982894 labels=set() properties={}>
 ) type='was_at' properties={'date': neotime.DateTime(2017, 1, 13, 0, 0, 0.0, tzinfo=<UTC>)}>>

我通过构建边列表、节点属性列表和边属性列表来创建 igraph 实例。

我在 Neo4j 3.5.14 的实例中运行这些查询。我的机器在带有 8 GB RAM 的 Intel Celeron @ 1.60 上运行 Ubuntu。

标签: pythonperformanceneo4j

解决方案


我找到了这个链接:

https://community.neo4j.com/t/performance-problem/1676

在那里,一位用户发现拥有 DateTime 属性会使该过程变得非常慢。

就我而言,将日期时间转换为纪元后,当我只返回一些属性时,时间提高到 20 秒。因此,我认为仍有改进的空间。

我会看一下并用调查结果更新这个答案。


推荐阅读