When trying to use OPTIONAL MATCHES in Cypher queries, you can often encounter timeouts (as a consequence of heavy queries).
2021-09-24 11:32:40.536 INFO 8 --- [nio-8080-exec-2] b.b.l.service.impl.ContactServiceImpl :
save or update contact with ab.cdef.linkedorganization.controller.domain.ContactAPI@12345f67
for organization 123456-abcd
2021-09-24 11:33:12.357 WARN 8 --- [/172.30.0.1/...] i.f.k.c.d.i.WatchConnectionManager :
Exec Failure
java.net.SocketTimeoutException: sent ping but didn't receive pong within 1000ms
(after 277 successful ping/pongs)
Multiple OPTIONAL MATCH clauses, one after the other is usually under performing (can create Cartesian products) because each OPTIONAL MATCH with all its occurrences gets injected in the internal result/working set.
Therefore a better solution would be the use of pattern comprehension.
Original Cypher query with multiple optional matches:
MATCH (o:ORGANIZATION) WHERE o.identifier = '123456-abcd'
OPTIONAL MATCH (o)-[h_rep:HAS_GDPR_REPRESENTATIVE]->(rep:CONTACT)
OPTIONAL MATCH (o)-[h_con:HAS_GDPR_CONTACT]->(con:CONTACT)
OPTIONAL MATCH (o)-[h_other:HAS_GDPR_OTHER_CONTACT]->(oCon:CONTACT)
OPTIONAL MATCH (o)-[h_c:HAS_FOR_CONTACT]->(c:CONTACT)
RETURN o, h_rep, rep, h_con, con, h_other, oCon, h_c, c
This can be rewritten using pattern comprehension:
MATCH (o:ORGANIZATION) WHERE o.identifier = '123456-abcd'
RETURN o, [
[(o)-[h_rep:`HAS_GDPR_REPRESENTATIVE`]->(rep:`CONTACT`) | [h_rep, rep]],
[(o)-[h_con:`HAS_GDPR_CONTACT`]->(con:`CONTACT`) | [h_con, con]],
[(o)-[h_other:`HAS_GDPR_OTHER_CONTACT`]->(oCon:`CONTACT`) | [h_other, oCon]],
[(o)-[h_c:`HAS_FOR_CONTACT`]->(c:`CONTACT`) | [h_c, c]]
]
Please check this documentation on Cypher Pattern comprehension for more details.
Comments
0 comments
Please sign in to leave a comment.