# Slow Cypher Query using CONTAINS and relationships attributes

**URL:** https://community.neo4j.com/t/slow-cypher-query-using-contains-and-relationships-attributes/12760
**Category:** Cypher
**Created:** [December 12, 2019, 12:23am UTC](https://community.neo4j.com/t/slow-cypher-query-using-contains-and-relationships-attributes/12760 "2019-12-12T00:23:07Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![cypherDMC](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cypherdmc/32/4090_2.png) [@cypherDMC](https://community.neo4j.com/u/cypherDMC)
#### Post date: [December 12, 2019, 12:23am UTC](https://community.neo4j.com/t/slow-cypher-query-using-contains-and-relationships-attributes/12760/1 "2019-12-12T00:23:07Z")

</div>

In our database we have a lot of nodes of type "Document"

```auto
MATCH (n:Document) RETURN COUNT(n) AS count

```

Returns

```auto
count
231266485

```

When queries are made using the operator = the response is fast

But we need to search only nodes containing number 8981231 and having relations with the following attributes:

```auto
MATCH (n:Document) WHERE (n.number CONTAINS '8981231') WITH n WHERE ((n)-[{someAttribute: 'linkAnalisysTest'}]-()) RETURN n.number LIMIT 10

```

Time to complete query

```auto
Started streaming 2 records after 10196 ms and completed after 44930 ms.

```

Explain (edited)

 ![explain-contains](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/6/695da4996a8d5d98b4e519b6aa90f6a4f2d17172.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: [December 12, 2019, 1:28am UTC](https://community.neo4j.com/t/slow-cypher-query-using-contains-and-relationships-attributes/12760/2 "2019-12-12T01:28:58Z")

</div>

Can you run a PROFILE instead, and also expand all elements of the query plan? It's the double-down arrow in the lower right of the result pane.

---

<div class="post-metadata">

### Author: ![cypherDMC](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cypherdmc/32/4090_2.png) [@cypherDMC](https://community.neo4j.com/u/cypherDMC)
#### Post date: [December 12, 2019, 11:50am UTC](https://community.neo4j.com/t/slow-cypher-query-using-contains-and-relationships-attributes/12760/3 "2019-12-12T11:50:59Z")

</div>

Hi! thanks for reply

Sorry for my mistake i just attached the wrong image

Explain (Correct) (Type of node is "cpf" i put "Document" to facilitate understanding

 ![explain-contains](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/6/695da4996a8d5d98b4e519b6aa90f6a4f2d17172.png)

Profile

 ![profile-contains](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/7/74ecfa51afc798bbd46695828144f254f364737b.png)

---

<div class="post-metadata">

### Author: ![mike\_r\_black](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/mike_r_black/32/30089_2.png) [@mike\_r\_black](https://community.neo4j.com/u/mike_r_black)
#### Post date: [December 12, 2019, 5:44pm UTC](https://community.neo4j.com/t/slow-cypher-query-using-contains-and-relationships-attributes/12760/4 "2019-12-12T17:44:31Z")

</div>

My guess is because the database can't do an exact match, reading from the beginning and moving on when the beginning of the attribute isn't a match, but instead the database has to examine the whole string of the attribute for the `CONTAIN` operation, you're seeing more db hits? This is only speculation.

Questions:

- Can you provide the **Profile** for both the `=` and the `CONTAINS` queries so we can compare the two explanations?
- Do you have any regular indexes specified on this attribute?
- Have you looked at specifying a [Full Text Search](https://neo4j.com/docs/cypher-manual/3.5/schema/index/#schema-index-fulltext-search) index on this data. If you're doing a fuzzy contains search, the Lucene index would be worth looking into.

---

<div class="post-metadata">

### Author: ![cypherDMC](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cypherdmc/32/4090_2.png) [@cypherDMC](https://community.neo4j.com/u/cypherDMC)
#### Post date: [December 12, 2019, 6:48pm UTC](https://community.neo4j.com/t/slow-cypher-query-using-contains-and-relationships-attributes/12760/5 "2019-12-12T18:48:13Z")

</div>

Hi mike! thanks for reply

Answer the questions

- **Indexes**  
Yes we have indexes in attributes (cpf and "someAttribute")

- **Full Text Search**  
We try use Full text search but the result is the same (slow query)

- Profile (=)

 ![profile-equals](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/4/4dc569857c1870361993d7ec1a030174737b3ebc.png)

- Profile (CONTAINS)  
 ![profile-contains](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/7/74ecfa51afc798bbd46695828144f254f364737b.png)

---

<div class="post-metadata">

### Author: ![cypherDMC](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cypherdmc/32/4090_2.png) [@cypherDMC](https://community.neo4j.com/u/cypherDMC)
#### Post date: [December 12, 2019, 9:58pm UTC](https://community.neo4j.com/t/slow-cypher-query-using-contains-and-relationships-attributes/12760/6 "2019-12-12T21:58:14Z")

</div>

HI guys

We try another approach and works!!!

```auto
CALL db.index.fulltext.queryNodes('document', '*95587*') yield node AS n WITH n WHERE ((n)-[{someAttribute: 'linkAnalisys'}]-()) RETURN n.number LIMIT 10

```
