Bolt connection is selectively slow with cypher queries while Web based GUI is always fast

315 Views Asked by At

I have two queries: q1 and q2. I use the code below to query my neo4j database.

driver = GraphDatabase.driver("bolt://localhost:7687",auth= neo4j_user,neo4j_password))
neo4j_session = driver.session()

t = time.time()
neo4j_session.run(q1,q1_parameters) 
print(time.time()-t)

t = time.time()
neo4j_session.run(q2,q2_parameters) 
print(time.time()-t)

both q1 and q2 are performed by neo4j in approximately 10 ms in the web interface (http://localhost:7474/browser/). The code above also executes q1 in approximately 10 ms but q2 is performed in 1s.

Why does neo4j bolt connection not like a specific query (q2) even though the web interface executes it 100 times faster? I believe this is not connection overhead as q1 is executed almost as fast either way

1

There are 1 best solutions below

1
On

You should not be creating a new driver and session for every query.

The README for the Python Bolt driver has the following "Quick Example" of how to run 4 queries (in 3 write and 1 read transactions) with the same driver and session:

from neo4j.v1 import GraphDatabase

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

def add_friends(tx, name, friend_name):
    tx.run("MERGE (a:Person {name: $name}) "
           "MERGE (a)-[:KNOWS]->(friend:Person {name: $friend_name})",
           name=name, friend_name=friend_name)

def print_friends(tx, name):
    for record in tx.run("MATCH (a:Person)-[:KNOWS]->(friend) WHERE a.name = $name "
                         "RETURN friend.name ORDER BY friend.name", name=name):
        print(record["friend.name"])

with driver.session() as session:
    session.write_transaction(add_friends, "Arthur", "Guinevere")
    session.write_transaction(add_friends, "Arthur", "Lancelot")
    session.write_transaction(add_friends, "Arthur", "Merlin")
    session.read_transaction(print_friends, "Arthur")