Weird Cypher error

20 views
Skip to first unread message

Mark Angrish

unread,
Oct 27, 2014, 6:47:33 PM10/27/14
to ne...@googlegroups.com
Hi there,

I have been trying to solve this bizarre problem for a day now and I can't figure it out. I have the following cypher:


MATCH (a:A{email:"a...@123.com"})
OPTIONAL MATCH
(a)-[r1:r1]-()
OPTIONAL MATCH
(a)-[r2:r2]-()
OPTIONAL MATCH
(a)-[r3:r3]-()
OPTIONAL MATCH
(a)-[r4:r4]-()
OPTIONAL MATCH
(a)-[r5:r5]-()
OPTIONAL MATCH
(a)-[r6:r6]-()
OPTIONAL MATCH
(a)-[problem:r7]-()
RETURN a
, r1, r2, r3, r4, r5, r6, r7


There is a unique constraint on A:email. All the other nodes have unique constraints too (usually on a uuid).

So I am using the REST API and passing in: 

"resultDataContents":["graph"]

This is to minimise the data sent over the wire and because I map the results to my domain objects. row based/jdbc sends way too much data and can't return properties on relationships AFAIK.

The problem i'm having is that by including the relationship problem:r7 my cypher query goes from around 1sec (without it) to around 40 sec (with it). The total data it needs to return is about 100-150 nodes.

I have ordered the relationships in order of reducing cardinality to make searching easier. If i do a count distinct return query for a:A-[problem:r7]-() the query returns in about 120ms.

Does anyone have any ideas? Also it might matter but it might not: I have upgraded the db several times from 2.1.3 -> 2.1.4 -> 2.1.5.



Mark Angrish

unread,
Oct 27, 2014, 8:55:04 PM10/27/14
to ne...@googlegroups.com

Ok a couple more hours of tinkering and I worked it out. Here is what i did in case any one else wants to follow this path of returning graphs back rather than rows:

MATCH (a:A{email:"a...@123.com"})-[rels:r1|r2|r3|r4|r5|r6|r7]-()
RETURN user
, rels

This effectively returns you all "optional relationships" that you are interested in. Returns in under around 100ms.

For what it's worth you can then structure more complexity over the top. For me i was interested in bringing down more nodes if a relationship condition was met.

MATCH (a:A{email:"a...@123.com"})-[rels:r1|r2|r3|r4|r5]-(n)
WITH a
, rels, n
OPTIONAL MATCH
(n:B)-[bRel:r8]-()
OPTIONAL MATCH
(n:C)-[cRel:r9]-()
RETURN a
, rels, bRel, cRel

::mark

Michael Hunger

unread,
Oct 27, 2014, 9:11:02 PM10/27/14
to ne...@googlegroups.com
The problem is just exploding cardinality. Each new mach multiplies the cardinality of the (intermediate) results

Your first solution is a good one

Another one that you might want to do is:

MATCH (a:A{email:"a...@123.com"})
OPTIONAL MATCH 
(a)-[r1:r1]-()
with a, collect(r1) as r1
OPTIONAL MATCH 
(a)-[r2:r2]-()
with a, r1, collect(distinct r2) as r2
....
OPTIONAL MATCH (a)-[problem:r7]-()

RETURN a
, r1, r2, r3, r4, r5, r6, collect(distinct propblem) as r7



--
You received this message because you are subscribed to the Google Groups "Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email to neo4j+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages