neo4j - 使用密码查询时,螺栓连接会选择性地变慢,而基于 Web 的 GUI 总是很快
问题描述
我有两个查询:q1 和 q2。我使用下面的代码来查询我的 neo4j 数据库。
driver = GraphDatabase.driver("bolt://localhost:7687",auth= neo4j_user,neo4j_password))
neo4j_session = driver.session()
t = time.time()
neo4j_session.run(q1,q1_parameters)
print(time.time()-t)
t = time.time()
neo4j_session.run(q2,q2_parameters)
print(time.time()-t)
q1 和 q2 均由 neo4j 在 Web 界面 ( http://localhost:7474/browser/ ) 中在大约 10 毫秒内执行。上面的代码也在大约 10 毫秒内执行 q1,但 q2 在 1 秒内执行。
为什么即使 web 界面执行速度快 100 倍,neo4j 螺栓连接也不像特定查询(q2)?我相信这不是连接开销,因为 q1 的执行速度几乎与任何一种方式一样快
解决方案
您不应该为每个查询创建新的驱动程序和会话。
Python Bolt 驱动程序的README包含以下“快速示例”,说明如何使用相同的driver
和运行 4 个查询(在 3 个写入和 1 个读取事务中) session
:
from neo4j.v1 import GraphDatabase
driver = GraphDatabase.driver("bolt://localhost:7687", auth=("neo4j", "password"))
def add_friends(tx, name, friend_name):
tx.run("MERGE (a:Person {name: $name}) "
"MERGE (a)-[:KNOWS]->(friend:Person {name: $friend_name})",
name=name, friend_name=friend_name)
def print_friends(tx, name):
for record in tx.run("MATCH (a:Person)-[:KNOWS]->(friend) WHERE a.name = $name "
"RETURN friend.name ORDER BY friend.name", name=name):
print(record["friend.name"])
with driver.session() as session:
session.write_transaction(add_friends, "Arthur", "Guinevere")
session.write_transaction(add_friends, "Arthur", "Lancelot")
session.write_transaction(add_friends, "Arthur", "Merlin")
session.read_transaction(print_friends, "Arthur")
推荐阅读
- typescript - 如何构建一个内部有数组对象的接口
- android - 为什么我的搜索框在 Android 应用地图活动中不起作用?
- javascript - 以下哪个代码是在 JavaScript 中使用 Promises 的正确方法?
- node.js - 无法在 Mern 堆栈应用程序中使用 axios.put 更新 mogodb 对象
- git - Git - 推送到镜像存储库时重写提交作者
- python - 请问如何在不覆盖现有键的情况下将新的键值对添加到字典中?
- javascript - 通过将常量导出为对象来丢失类型信息
- reactjs - Uncaught SyntaxError: Unexpected token '<' while deploy create react app to firebase hosting
- javascript - Facebook Marketing API JavaScript 如何获取响应标头 x-business-use-case-usage
- assembly - 无法获得汇编语言代码的输出