J. *_*tty 4 python neo4j cypher neo4j-driver
我试图比较从 python 获取 Cypher 查询的执行时间,即在 neo4j 服务器上计算所需的时间(不包括输出结果所需的时间)。现在我正在使用以下代码:
from neo4j.v1 import
driver = GraphDatabase.driver('bolt://localhost:7687', auth=('neo4j', '1234'))
n_repeats = 3
cypher = "MATCH (a) -[:{}*]- (b) WHERE ID(a) < ID(b) RETURN DISTINCT a, b".format(graphname + '_edges')
with driver.session() as session:
total_time = 0
for _ in range(n_repeats):
with session.begin_transaction() as tx:
start = time.time()
tx.run(cypher)
total_time += time.time() - start
avg_time = total_time*1000 / n_repeats
print('Average execution time:', avg_time, 'ms')
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来计算密码查询的执行时间?例如,在 postgresql 中,有 EXPLAIN ANALYZE 语句,它还提供了执行 SQL 查询所需的时间。在 Cypher 中有 EXPLAIN 和 PROFILE 语句,但两者似乎都没有返回特定时间。
我现在正在使用 neo4j-driver 连接到 neo4j,但我愿意切换到另一个库。
事实上,所用时间在所有结果中都可用,无需进行分析。它们位于结果摘要中,执行时间分为任何结果流可用之前的时间和服务器消耗整个结果流之前的时间。
可以将它们加在一起以获得查询的总执行时间,以毫秒为单位:
result = tx.run(query, params)
avail = result.summary().result_available_after
cons = result.summary().result_consumed_after
total_time = avail + cons
Run Code Online (Sandbox Code Playgroud)