python - 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。
解决方案
我找到了这个链接:
https://community.neo4j.com/t/performance-problem/1676
在那里,一位用户发现拥有 DateTime 属性会使该过程变得非常慢。
就我而言,将日期时间转换为纪元后,当我只返回一些属性时,时间提高到 20 秒。因此,我认为仍有改进的空间。
我会看一下并用调查结果更新这个答案。
推荐阅读
- java - 覆盖子类中的父方法。子方法不做任何事情
- google-apps-script - 使用身份验证从 Dialogflow 向 Appscript 发送请求
- javascript - 使用 React 的计时器应用程序:每次渲染后都会清除 setInterval
- jmeter - 如何为线程组中的单个请求实现 10tps 吞吐量
- python - 使用 BeautifulSoup 从网站中抓取每个元素
- c# - 如何处理 System.Windows.Media.Brushes
- python - 为什么 f-strings 格式不适用于 Pandas DataFrames?
- angular - 如何在使用 Angular innerHtml 时保持样式?
- php - PHP 警告:ftp_login():先用 USER 登录
- wix - 错误“系统管理员已设置策略以阻止此安装”,同时使用 perUser 安装实施 MSI 包