Show db hits for query using Python Driver

Hi Neo4j Community,

Thank you very much for always being so helpful. I am just getting starting with Python Driver and I would like to meassure the performance of the queries I execute.

For this I would like to know the total db hits for the queries I am executing just like in Neo4j directly using PROFILE.

I have read through various posts and searched and read through manuals and documentations and feel like I am close, but not quite there.

My python code looks as follows

and I get the output

which has all the relevant db hits in there. I am just wondering if there is a nicer way to get the total and trying to access this information from ......consume().profile could this not be different again for a different query and I would first have to look through the result and see how to access the single db hits information and add them up.

Just wondering if there is an easy way to access the 37 db hits (in this example) in an easier way?

Thank you very much for your help.


Hi Philipp,

currently, there is no convenience function to sum up all db hits for you. And the profile being this tree structure is just what the driver gets from the DBMS. However, you can write a little function that does it for you. It could look something like this:

def sum_db_hits(profile):
    return (profile.get("dbHits", 0)
            + sum(map(sum_db_hits, profile.get("children", []))))

In context:

import neo4j

def profile_query(tx, query, **params):
    # make sure the DBMS gives us a profile back.
    query_lower = query.strip().lower()
    if not (query_lower.startswith("profile")
            or query_lower.startswith("explain")):
        query = "PROFILE " + query
    result =, params)
    return result.consume().profile

def sum_db_hits(profile):
    return (profile.get("dbHits", 0)
            + sum(map(sum_db_hits, profile.get("children", []))))

def main():
    auth = ('neo4j', 'pass')
    uri = 'neo4j://localhost:7687'
    with neo4j.GraphDatabase.driver(uri, auth=auth) as driver:
        with driver.session() as session:
            profile = session.read_transaction(
                "<YOUR QUERY HERE>"

if __name__ == "__main__":

Hi Rouven,

Thank you very much for the detailed answer, much appreciated.

Good to know that this workaround is the best solution at this moment and I will make sure to use it.