【发布时间】:2017-03-11 06:19:05
【问题描述】:
出于测试目的,我使用的是具有以下规格的 Windows 7 笔记本电脑:
我还使用 neo4j-enterprise-3.0.2 并使用 BOLT 连接将我的 Pycharm python 程序连接到数据库。我创建了许多节点和关系。我注意到节点和关系的创建在特定时间后会大大减慢,并且在特定点后几乎没有进展。
我检查了以下内容
- 我对节点和属性使用唯一约束,以便架构索引可用于轻松查找数据库中的节点
- 我注意到我的 RAM 内存在所有这些事务发生时不断增加。我在 dbms.memory.pagecache.size 的 neo4j 配置文件中使用了不同的设置(默认,2g、3g、10g),它们都导致我的 RAM 从大约 4GB(没有运行 python 代码)增加到 7GB 及以上。那是节点的创建变得非常缓慢的时候。停止程序时,RAM 使用量再次下降。
这是运行状况监视器向我显示的内容:
问题:为什么节点和关系的创建速度这么慢?是因为图形大小(但数据集似乎相当小)吗?它与数据库的 BOLT 连接和事务有关吗?它与增加的 RAM 使用量有关吗?如何预防?
我创建了这个简单的例子来说明问题:
from neo4j.v1 import GraphDatabase
#BOLT driver
driver = GraphDatabase.driver("bolt://localhost")
session = driver.session()
#start with empty database
stmtDel = "MATCH (n) OPTIONAL MATCH (n)-[r]-() DELETE n,r"
session.run(stmtDel)
#Add uniqueness constraint
stmt = 'CREATE CONSTRAINT ON (d:Day) ASSERT d.name IS UNIQUE'
session.run(stmt)
# Create many nodes - run either option 1 or option 2
# # Option 1: Creates a node one by one. This is slow in execution and keeps the RAM flat (no increase)
# for i in range(1,80001):
# stmt1 = 'CREATE (d:Day) SET d.name = {name}'
# dict = {"name": str(i)}
# print(i)
# session.run(stmt1,dict)
#Option 2: Increase the speed and submit multiple transactions at the same time - e.g.1000
# This is very fast but blows up the RAM memory in no time, even with a limit on the page cache of 2GB
tx = session.begin_transaction()
for i in range(1, 80001):
stmt1 = 'CREATE (d:Day) SET d.name = {name}'
dict = {"name": str(i)}
tx.run(stmt1, dict) # append a transaction to the block
print(i)
if divmod(i,1000)[1] == 0: #every one thousand transactions submit the block and creat an new transaction block
tx.commit()
tx.close
tx = session.begin_transaction() #it seems that recycling the session keeps on growing the RAM.
【问题讨论】:
-
能分享一下python代码吗?
-
威廉,我在问题中添加了一个简单的例子。这显示了导致问题的原因。我相信这是与交易相关的。我一定是做错了什么。
-
顺便说一句。在您设置 250M 或最多 1G 的页面缓存时应该足够好。这似乎与 python 驱动程序的 tx/session mgmt 有关。你用的是哪个版本?
-
对于我的 Pycharm 项目,使用的 python 解释器是 3.4.3,neo4j-driver 是 1.0.1 版本
-
我实际上切换回 py2neo 包进行批量事务处理,并且工作正常而不会超载我的内存。我相信neo4j.v1包和内存管理肯定有问题。
标签: python graph neo4j pycharm ram