# Tuning of cypher queries for List operations

**URL:** <https://community.neo4j.com/t/tuning-of-cypher-queries-for-list-operations/43628>\
**Category:** Cypher\
**Tags:** optimization\
**Created:** [August 29, 2021, 11:45am UTC](https://community.neo4j.com/t/tuning-of-cypher-queries-for-list-operations/43628 "2021-08-29T11:45:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dhanashree.murge](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/dhanashree.murge/32/4568_2.png) [@dhanashree.murge](https://community.neo4j.com/u/dhanashree.murge)\
**Post date:** [August 29, 2021, 11:45am UTC](https://community.neo4j.com/t/tuning-of-cypher-queries-for-list-operations/43628/1 "2021-08-29T11:45:13Z")

</div>

I have imported one RDF file in Neo4J using NeoSemantics.  
Total no of nodes : 1071461 (1 Million)  
Total Relationships : 2553482 (2.5 Million)  
When I query this imported graph for traversal queries, I usually get execution time arount 1-100 milliseconds, But when I go for using list in my queries, execution time is quite high ranging from 1.5-5 seconds.

Ex of list query: (having response time within 1.5-5 seconds):

> Match (n:Resource) where n.skos\_\_prefLabel ="Anti-Allergic Agents" with n.skos\_\_notation AS dm\_notation match (n) where dm\_notation IN n.ns3\_\_PA return n.skos\_\_prefLabel

Ex of traversal query (having response time within 1-100 ms):

> match (n:Resource{skos\_\_prefLabel:'metabolism'})\<-[:rdfs\_\_subClassOf]-(p) return p.skos\_\_prefLabel

Is there any way to bring down execution time of such queries?  
Any help would be appreciated, Thanks in advance!  
P.S. :  
I have added single property indexes for Resource nodes on skos\_\_prefLabel, skos\_\_notation and ns3\_\_PA property.

---

<div class="post-metadata">

**Author:** ![kaiserbergin](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/kaiserbergin/32/19468_2.png) [@kaiserbergin](https://community.neo4j.com/u/kaiserbergin)\
**Post date:** [September 2, 2021, 4:54am UTC](https://community.neo4j.com/t/tuning-of-cypher-queries-for-list-operations/43628/2 "2021-09-02T04:54:57Z")

</div>

I think it makes sense that you have quite a big difference in performance here, for a few reasons:

- Property searches are always going to be slower than label / relationship searches.

- Your `WITH` clause specifies a property on the resource, but anything not in your `WITH` clause won't be ported over. Since you aren't, your `MATCH (n)` literally does a comparison on every node in your entire graph as `n` is undeclared at that point. I am unsure if this is intentional or not.

- Also, you're still at a bad spot because you're taking each row's property and doing a comparison against EVERY other row EACH time. You could _probably_ improve it with something like (I'm tired so someone else please help out):

```auto
MATCH (n:MyLabel { myFilterProp: "someValue" })
WITH collect(DISTINCT n.myPropToCheck) AS checkList
MATCH (n:MyLabel) 
WHERE any(prop IN n.listProp WHERE prop IN checkList)

```

---

<div class="post-metadata">

**Author:** ![dhanashree.murge](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/dhanashree.murge/32/4568_2.png) [@dhanashree.murge](https://community.neo4j.com/u/dhanashree.murge)\
**Post date:** [September 2, 2021, 6:36am UTC](https://community.neo4j.com/t/tuning-of-cypher-queries-for-list-operations/43628/3 "2021-09-02T06:36:44Z")

</div>

Thank you for the suggestion, it did improve my response time, and came down to 900 ms from 1.5 seconds! But it still is higher than expected execution time. I agree with you on the fact that this will take longer than relationship searches and want to make sure to bring down execution time as much as possible. With RDF4J I get response time for such queries in range 30-100 ms.

```auto
MATCH (n:Resource { skos__prefLabel : "Anti-Allergic Agents" })
WITH n.skos__notation AS dm_notation
MATCH (m:owl__Class) WHERE any(prop IN m.ns3__PA WHERE prop=dm_notation)
RETURN m.skos__prefLabel 
```

Can this query be further optimized? Also does response time depend on memory configuration of neo4j? I have maximum heap size set to default 512MB

---

<div class="post-metadata">

**Author:** ![Bennu](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/bennu/32/11659_2.png) [@Bennu](https://community.neo4j.com/u/Bennu)\
**Post date:** [September 2, 2021, 8:01am UTC](https://community.neo4j.com/t/tuning-of-cypher-queries-for-list-operations/43628/4 "2021-09-02T08:01:25Z")

</div>

Well,

How many elements are usually in this list? Is there any option you turn it into a Label? You can always make relationships to a new node that represent this property-value for the whole model. If you aim for the performance you may need to adjust your into something more Graphish.

Bennu
