Avoid cartesian product when create relationships

If you collect them between matching them, sure:

MATCH (a {property1:0})
WITH collect(a) as aNodes
MATCH (b {property2:1})
WITH aNodes, collect(b) as bNodes

(also you should be using labels, otherwise these will be expensive allNodesScans, two of them).

So now you have a list of 100 a nodes and a list of 100 b nodes. To connect them to each other you either need two foreaches (one nested in the other), or to UNWIND both lists back to rows. That would get you a cartesian product as a result, but without doing the extra work from the back-to-back matches, then you create your relationships.

1 Like