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

J. Gambolputty Source

I am trying to compare get the execution time of a Cypher query from python, i.e. the time required to compute on the neo4j-server (not including the time required output the result). 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()
            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. In Cypher there are EXPLAIN and PROFILE statements, but both seemingly don't return specific times.

I am using neo4j-driver to connect to neo4j right now, but I would be willing to switch to another library.



answered 5 months ago Rebecca Nelson #1

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 =, params)
avail = result.summary().result_available_after
cons = result.summary().result_consumed_after
total_time = avail + cons

comments powered by Disqus