0
votes

I want to obtain all wallet nodes except for those in wallet-pairs with only one :SendTo relationship between them and no :SendTo relationships to any other wallets. Is this query correct for that purpose?:

MATCH (n:Wallet)
OPTIONAL MATCH (n)-[r1:SendTo]-(n1:Wallet)-[r12:SendTo]-(n2:Wallet)
WHERE n <> n1
WITH n,count(n1) + count(n2) as rels
OPTIONAL MATCH (n3:Wallet)-[r2:SendTo]-(n)-[r22:SendTo]-(n4:Wallet)
WHERE n <> n3  
WITH n, rels, count(n3) + count(n4) as rels1
WHERE rels1 > 0 or rels>0
return n

From the test I have done, I would say that it is correct. But I would prefer a second opinion.

AS to how the graph is created: each wallet node is connected to another wallet node or itself via one or more unidirectional :SendTo relationships. It is possible to have more than one such relationship between the same two wallet nodes.

1
Hello! Is a good idea add to your question a initial data mass (A list of Cypher CREATE commands, maybe a draw of your graph) for another users help you in a more efficient way!Bruno Peres
I just added a textual description of the graph.Aqqqq
You may find it helpful to get relationship degrees from your nodes. Something like WITH n, size((n)-[:SendTo]-()) as degree WHERE degree > 1 AND ...InverseFalcon

1 Answers

2
votes

Your query does a lot to ask a simple question "Find all wallets with 0 or 2+ edges". So even if it is technically correct (too hard to read, too much going on), it will perform slowly. So what you really want is

MATCH (w:Wallet)
WITH w, size((n)-[:SendTo]-()) as edges
WHERE edges = 0 OR 1 < edges
RETURN w

(as a general rule, if you aren't sure what something is doing, it is either wrong, or someone will try to 'fix' it and make it wrong)