简体   繁体   English

如何从python获取密码查询的执行时间?

[英]How to get the execution time of a cypher query from python?

I am trying to compare get the execution time of a Cypher query from python, ie the time required to compute on the neo4j-server (not including the time required output the result).我试图比较从 python 获取 Cypher 查询的执行时间,即在 neo4j-server 上计算所需的时间(不包括输出结果所需的时间)。 Right now I am using the following code:现在我正在使用以下代码:

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')

Is there a better way to time the execution time of a cypher query?有没有更好的方法来计算密码查询的执行时间? In postgresql for instance there is the EXPLAIN ANALYZE statement, which also provides the time required to execute a SQL query.例如,在 postgresql 中有 EXPLAIN ANALYZE 语句,它也提供了执行 SQL 查询所需的时间。 In Cypher there are EXPLAIN and PROFILE statements, but both seemingly don't return specific times.在 Cypher 中有 EXPLAIN 和 PROFILE 语句,但两者似乎都没有返回特定时间。

I am using neo4j-driver to connect to neo4j right now, but I would be willing to switch to another library.我现在正在使用 neo4j-driver 连接到 neo4j,但我愿意切换到另一个库。

In fact, the time taken is available in all results without profiling.事实上,所用时间在所有结果中都可用,无需分析。 They are in the summary of the result, and the execution time is split into the time until any of the results stream is available and the time until the entire results stream has been consumed by the server.它们位于结果的摘要中,执行时间分为任何结果流可用之前的时间和服务器消耗整个结果流之前的时间。

They can be added together to get the total execution time of the query, expressed in milliseconds:它们可以相加得到查询的总执行时间,以毫秒为单位:

result = tx.run(query, params)
avail = result.summary().result_available_after
cons = result.summary().result_consumed_after
total_time = avail + cons

Neo4j Python Driver 1.7 Neo4j Python 驱动程序 1.7

from neo4j.v1 import 
driver = GraphDatabase.driver("bolt://localhost:7687", auth=("neo4j", "password"))

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()
            result = tx.run(cypher)
            records = list(result)  # consume the records
            tx.commit()
            total_time += time.time() - start

avg_time = total_time*1000 / n_repeats
print('Average execution time:', avg_time, 'ms')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM