Nicolle Madalina
Nicolle Madalina

Reputation: 13

Neo4j query performance improvement

I am quite new with Neo4J and I am using a query which takes really long time to be completed. The query looks like:

match (pn:person_name)-[:FOR_PERSON]->(p:person)<-[:FOR_PERSON]-(o:obs)-[:HAS_CONCEPT]->(c:concept)<-[:HAS_CONCEPT]-(n:concept_name), (o)-[:AT_LOCATION]->(l:location),

(o)-[:FOR_ENCOUNTER]->(e:encounter)-[:HAS_TYPE]->(et:encounter_type),(c)-[:HAS_CLASS]->(cl:concept_class), 

(c)<-[r:HAS_CONCEPT]-(cd:concept_description)

where pn.given_name="Horatio" AND  pn.middle_name="L" and pn.family_name="Hornblower" with pn, p, o, c,n,e,et,cl,cd,l

return distinct n.name as observation, o.obs_datetime as time, et.description as visittype, l.name as location,cl.name as encountertype,cd.description as description 

ORDER BY o.obs_datetime

I tried to profile it and it appears that when it looks for concept_description, all possible nodes are returned, not only the one that matches our previous matched concept. Any ideas what I m doing wrong? Thank you!

Upvotes: 1

Views: 65

Answers (1)

binariedMe
binariedMe

Reputation: 4329

I will try explaining the mechanism of neo4j query :

when you do :

MATCH (a:X)-[]-(b:Y), (b:Y)-[]-(c:Z) where b.someField = someValue

both match will run independently but when you do something like this :

MATCH (a:X)-[]-(b:Y) where b.someField = someValue with b match (b)-[]-(c:Z) return a,b,c

second query will be dependent on the outcome of first query and hence cascading will return you correct result and will be more efficient too.

Upvotes: 1

Related Questions