Cypher query slow performance

Hello there, i need recommendations about Cypher queries.
Honestly, dont know how to make it better, but it still awfully slow.
Full text of query:

With production database, full API request executes near of 3 seconds. And i can't figure out where exactly the problem: the query, the method of connection on backend level or even database setup (maybe i should optimize database itself?)

  • neo4j version:4.4.21
  • Python driver, from neo4j import GraphDatabase
  • No plugins or any additional stuff
  • Community version, so i dont have any logs.

I don't know where exactly the problem: the query itself, the database setup or even backend connection method. Any tips will be useful.
Maybe i should split this big query on the smaller and async ones?

Hello @antonigin1995 :slight_smile:

Can you share the graph data model?

Best regards,
Cobra

Hello, here is the database diagram:

What is the aim of the query?

Maybe you could replace OPTIONAL MATCH with pattern comprehension or subqueries. I also see an OPTIONAL MATCH with an infinite depth, you could use apoc.path.expandConfig() since you are on Neo4j 4.4 (with Neo4j 5.9, you could use quantified path patterns). Infinite depth can cause performance issue especially if there are densely connected nodes.

Cobra

My goal is to show part of the nodes related with Catalog Node - content, domains, skills etc.
Thanks for advices, gonna try them and reply here about results.

Unfortunately, even very-very simple case of query working very slow, i am literally have no idea why it happens. Example:

1.2 seconds for requesting 2 nodes and retrieving one of them. After script restart, it goes from 1.2 to 0.02, but it's because of the cache, so doesn't count.

Why it happens? Where am i mistaken - the query, the python implementation? Should i switch something in database settings?