# “Query Optimization” : Neo4j Query builds a cartesian product between disconnected patterns -

**URL:** <https://community.neo4j.com/t/query-optimization-neo4j-query-builds-a-cartesian-product-between-disconnected-patterns/26458>\
**Category:** Cypher\
**Tags:** performance, cypher, relationship\
**Created:** [October 2, 2020, 6:34am UTC](https://community.neo4j.com/t/query-optimization-neo4j-query-builds-a-cartesian-product-between-disconnected-patterns/26458 "2020-10-02T06:34:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alishahaan9](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/alishahaan9/32/4376_2.png) [@alishahaan9](https://community.neo4j.com/u/alishahaan9)\
**Post date:** [October 2, 2020, 6:34am UTC](https://community.neo4j.com/t/query-optimization-neo4j-query-builds-a-cartesian-product-between-disconnected-patterns/26458/1 "2020-10-02T06:34:53Z")

</div>

I’m supposed to have graph of multiple nodes(more than 2) with their relationships at 1st degree, second degree, third degree.

**For that right now I am using this query**

```auto
WITH ["1258311979208519680","3294971891","1176078684270333952",”117607868427845”] as ids 
MATCH (n1:Target),(n2:Target) WHERE n1.id in ids and n2.id in ids and n1.id<>n2.id and n1.uid=103 and n2.uid=103 
MATCH p = ((n1)-[*..3]-(n2)) RETURN p limit 30

```

In which 4 nodes Id’s are mention in **WITH[]** and next **[\*..3]** it is used to draw **3rd degree graph** between the selected nodes.

**WHAT the ABOVE QUERY DOING**

After running the above query it will return the mutual nodes in case of second degree [\*..2] if any of the 2 selected nodes have mutual relation it’ll return.

**WHAT I WANT**

1. First of all I want to optimize the query, as it is taking so much time and this query causing the Cartesian product which slow down the query process.

2. As in this above query if any 2 nodes have mutual relationship it will return the data, I WANT, the query will return **mutual nodes** attached with all selected nodes. Means if we have some nodes in return, these nodes must have relation to all selected target nodes.\*

Any suggestions to modify the query, to optimize the query.

Stackoverflow link : [cypher - "Query Optimization" : Neo4j Query builds a cartesian product between disconnected patterns - - Stack Overflow](https://stackoverflow.com/questions/64167019/query-optimization-neo4j-query-builds-a-cartesian-product-between-disconnect)

---

<div class="post-metadata">

**Author:** ![ameyasoft](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/ameyasoft/32/32639_2.png) [@ameyasoft](https://community.neo4j.com/u/ameyasoft)\
**Post date:** [October 2, 2020, 8:14pm UTC](https://community.neo4j.com/t/query-optimization-neo4j-query-builds-a-cartesian-product-between-disconnected-patterns/26458/2 "2020-10-02T20:14:00Z")

</div>

```auto
Took the result suggested in the above Stackoverflow link and modifying it:

MATCH (node1:Target) WHERE node1.id IN ["1258311979208519680","3294971891","1176078684270333952"]
MATCH (node2:Target) WHERE node2.id IN ["1258311979208519680","3294971891","1176078684270333952"]

MATCH p=(node1)-[*..2]-(node2) 
where node1.id <> node2.id and node1.uid = 103 = node2.uid
RETURN p

```

---

<div class="post-metadata">

**Author:** ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)\
**Post date:** [October 3, 2020, 8:44am UTC](https://community.neo4j.com/t/query-optimization-neo4j-query-builds-a-cartesian-product-between-disconnected-patterns/26458/3 "2020-10-03T08:44:34Z")

</div>

Hello @alishahaan9 and @ameyasoft 🙂

I think we can also move the last `WHERE` clause:

```auto
MATCH (node1:Target)
WHERE node1.id IN ["1258311979208519680","3294971891","1176078684270333952"]
MATCH (node2:Target)
WHERE node2.id IN ["1258311979208519680","3294971891","1176078684270333952"]
WITH node1, node2
WHERE node1.id <> node2.id and node1.uid = 103 = node2.uid
MATCH p=(node1)-[*..2]-(node2)
RETURN p

```

Regards,  
Cobra

---

<div class="post-metadata">

**Author:** ![huzaifa.4041](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/huzaifa.4041/32/6370_2.png) [@huzaifa.4041](https://community.neo4j.com/u/huzaifa.4041)\
**Post date:** [October 5, 2020, 7:13am UTC](https://community.neo4j.com/t/query-optimization-neo4j-query-builds-a-cartesian-product-between-disconnected-patterns/26458/4 "2020-10-05T07:13:19Z")

</div>

This query will solve the cartesian product issue but it will return the mutual nodes if any of the two nodes have some mutual nodes.  
But how can this be changed as he asked, the query should return the relationship only if it is linked with all the selected nodes (in array).  
Means if we want to create the second degree graph there should be the mutual nodes which are connected with all the target nodes.
