Memory errors while printing query results in python

Hello everyone,
having some issues with memory management on neo4j.
Here's the simple graph that I've been using:
image

I have a total of 9,5k nodes and 2kk relationships.
I'm trying to find two Customers in a relationship of degree 3.

Here's my cypher query:
MATCH (u:Customer)-[:Transaction]->(:Terminal)<-[:Transaction]-(u1:Customer)-[:Transaction]->(:Terminal)<-[:Transaction]-(u2:Customer)
WHERE u.CUSTOMER_ID <> u1.CUSTOMER_ID AND u1.CUSTOMER_ID <> u2.CUSTOMER_ID AND u.CUSTOMER_ID <> u2.CUSTOMER_ID
RETURN u.CUSTOMER_ID, u2.CUSTOMER_ID

Using it on Neo4j Desktop it crashes after 40 minutes.

So, I was trying to print the result in a python script.
with driver.session() as session:
results = session.run(cypher_query)
for record in results:
print(record)

But even like this I'm getting a MemoryPoolOutOfMemoryError exceeding the 2,8 GiB limit.

I tried using apoc with the CALL apoc.export.csv.query command, but I'm getting the same error.

I'm pretty new to neo4j so I have a few questions:

  1. is it normal that the neo4j browser just crashes on me?
  2. How can I handle all the memory errors? Can i iterate somehow on the result of the query?

Thank you all.

Desktop becomes effectively unresponsive with large result sets. Try adding a ‘Limit 10’ after return to ensure the query is working as expected.

You can try cypher-shell to see if it is any better.

Thank you I tried both with Limit and using the cypher shell, I get the same results of the desktop .

With LIMIT 1000 I get the result in less than a second with the correct answer.
Without limit I get a memory error with both desktop and cypher-shell.

I'm wondering if is there a way to stream the result or access it in batches. How can i write the entire result on a file without getting a memory error? Is it even possible?

I'm having a hard time understanding how to use the full result of these neo4j queries.

Also, running this python script:
with driver.session() as session:
start_time=datetime.datetime.now()
results = session.run(cypher_query)
print("Elapsed: {}".format( (datetime.datetime.now()-start_time).total_seconds()/60.0))

if I don't print the result, the program exits in 0,03 seconds.
Does it mean the query only takes 0,03 seconds? But then why does it run out of memory when I try to print it?