# Cartesian product while referencing the same relation with different constraints

**URL:** <https://community.neo4j.com/t/cartesian-product-while-referencing-the-same-relation-with-different-constraints/16399>\
**Category:** Cypher\
**Tags:** performance, cypher\
**Created:** [March 29, 2020, 6:15am UTC](https://community.neo4j.com/t/cartesian-product-while-referencing-the-same-relation-with-different-constraints/16399 "2020-03-29T06:15:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![angkirat](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/angkirat/32/10182_2.png) [@angkirat](https://community.neo4j.com/u/angkirat)\
**Post date:** [March 29, 2020, 6:15am UTC](https://community.neo4j.com/t/cartesian-product-while-referencing-the-same-relation-with-different-constraints/16399/1 "2020-03-29T06:15:00Z")

</div>

I am completely new to Neo4j. The current graph version is **4.0.1 Enterprise**. The issue being faced by me is that of cypher query.  
The data can be viewed using the following [link](https://drive.google.com/file/d/1AWkOtsmdemODoOL-gVAyKzKQthLypEcn/view?usp=sharing)  
Below are some screenshots that help understand the data.  
`call db.schema.visualization()`  
 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/1/175bdf6055d6f97687454240fed7966097b711b9.png)  
_one Person above can be linked only to one type._

 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/8/8c01d284800ad83058e2a6666e7e6f5535ef2dd7.png)  
This image shows the person count per type.

This is the part where I am facing the issue

```auto
MATCH (p1:PERSON)<-- (:TYPE{name:"t1"})
MATCH (P2:PERSON)<-- (:TYPE{name:"t2"})
RETURN COUNT(p1), COUNT(P2)

```

Here I get the result as 1786(38\*47) for both the counts. I need the first column to have value 38 and the other to have 47.

**P.S.:** This is a dummy problem recreated to mimic a real-world situation of creating a self-join of one person with another from the other type. The CSV load is taking too much time thus even though that is a simpler way the computation time is so much that the desktop is not able to process it even though I have set heap space of 8GB space.

Thanks in advance for the help.

---

<div class="post-metadata">

**Author:** ![jaini.kiran](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/jaini.kiran/32/9706_2.png) [@jaini.kiran](https://community.neo4j.com/u/jaini.kiran)\
**Post date:** [March 29, 2020, 2:09pm UTC](https://community.neo4j.com/t/cartesian-product-while-referencing-the-same-relation-with-different-constraints/16399/2 "2020-03-29T14:09:02Z")

</div>

```auto
MATCH (p1:PERSON)<-- (:TYPE{name:"t1"})
WITH COUNT(p1) as p1_count
MATCH (P2:PERSON)<-- (:TYPE{name:"t2"})
RETURN p1_count, count(p2) as p2_count

```

You can try this.

---

<div class="post-metadata">

**Author:** ![angkirat](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/angkirat/32/10182_2.png) [@angkirat](https://community.neo4j.com/u/angkirat)\
**Post date:** [March 30, 2020, 6:14am UTC](https://community.neo4j.com/t/cartesian-product-while-referencing-the-same-relation-with-different-constraints/16399/3 "2020-03-30T06:14:04Z")

</div>

Thank you for the help @jaini.kiran !!  
Does this have a constraint that it can only be used for aggregation or single result cause I am not able to return the nodes in this scenario thus Relation building is becoming challenging
