Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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, 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()
            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. 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.

like image 513
J. Gambolputty Avatar asked Oct 20 '25 21:10

J. Gambolputty


1 Answers

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
like image 165
Rebecca Nelson Avatar answered Oct 22 '25 11:10

Rebecca Nelson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!