# Efficiently filter nodes which have multiple relationships

**URL:** <https://community.neo4j.com/t/efficiently-filter-nodes-which-have-multiple-relationships/4853>\
**Category:** Cypher\
**Created:** [February 5, 2019, 6:54am UTC](https://community.neo4j.com/t/efficiently-filter-nodes-which-have-multiple-relationships/4853 "2019-02-05T06:54:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![m-kiuchi](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/m-kiuchi/32/577_2.png) [@m-kiuchi](https://community.neo4j.com/u/m-kiuchi)\
**Post date:** [February 5, 2019, 6:54am UTC](https://community.neo4j.com/t/efficiently-filter-nodes-which-have-multiple-relationships/4853/1 "2019-02-05T06:54:05Z")

</div>

Hi all,

I have a graph data with millions of nodes which is grouped with property. For several reason, I don't use node label.  
When I filter nodes which have multiple relationships, Neo4j returned "Out of memory" error and I cannot complete my query like this.

```auto
MATCH (n) WHERE n.idtype='ip'
WITH n
MATCH p=(n)-[r:have_ip]->()
WITH count(r) as cntr, p
WHERE cntr>2
RETURN p LIMIT 100

```

```auto
Neo.TransientError.General.OutOfMemoryError: There is not enough memory to perform the current task. Please try increasing 'dbms.memory.heap.max_size' in the neo4j configuration (normally in 'conf/neo4j.conf' or, if you you are using Neo4j Desktop, found through the user interface) or if you are running an embedded installation increase the heap by using '-Xmx' command line flag, and then restart the database.

```

How do I complete my query efficiently (without tweaking heap size) ?  
Any comment is welcome !

---

<div class="post-metadata">

**Author:** ![m-kiuchi](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/m-kiuchi/32/577_2.png) [@m-kiuchi](https://community.neo4j.com/u/m-kiuchi)\
**Post date:** [February 5, 2019, 7:36am UTC](https://community.neo4j.com/t/efficiently-filter-nodes-which-have-multiple-relationships/4853/2 "2019-02-05T07:36:58Z")

</div>

Resolved by myself... Haha.

```auto
MATCH (n) WHERE n.idtype='ip'
WITH n
MATCH p=(n)-[r:have_ip]->()
WITH count(r) as cntr, n
WHERE cntr>2
WITH n
MATCH p=(n)-[:have_ip]->()<--()
RETURN p LIMIT 50

```

 ![Screenshot%20from%202019-02-05%2016-35-59](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/1/1225b94beb4d634ff5068222fbd3bb82f390c099.png)

---

<div class="post-metadata">

**Author:** ![andrew\_bowman](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/andrew_bowman/32/73_2.png) [@andrew\_bowman](https://community.neo4j.com/u/andrew_bowman)\
**Post date:** [February 5, 2019, 7:37am UTC](https://community.neo4j.com/t/efficiently-filter-nodes-which-have-multiple-relationships/4853/3 "2019-02-05T07:37:43Z")

</div>

For one, you really should be using labels, AllNodesScans are expensive.

Second, you can use the `size()` of a pattern with just the relationship type and/or direction to get the degree of a relationship without paying the cost of expanding it, that's a more efficient way to get the info you need.

```auto
MATCH (n)
WHERE n.idtype='ip' AND size((n)-[:have_ip]->()) > 1
WITH n
LIMIT 50 // every node would have at least 2 relationships, so at least 100 paths total
MATCH p = (n)-[:have_ip]->()
RETURN p
LIMIT 100

```

---

<div class="post-metadata">

**Author:** ![m-kiuchi](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/m-kiuchi/32/577_2.png) [@m-kiuchi](https://community.neo4j.com/u/m-kiuchi)\
**Post date:** [February 5, 2019, 7:38am UTC](https://community.neo4j.com/t/efficiently-filter-nodes-which-have-multiple-relationships/4853/4 "2019-02-05T07:38:49Z")

</div>

So informative. Thanks much !
