sabbir
sabbir

Reputation: 2025

Neo4j: Improve cypher performance

I have the following cypher query where I have to UNWIND around 100 data. But my problem is, to run query, it takes so much time to execute (about 3-4 mins).

My Query:

CREATE (pl:List {id: {id}, title: {title} })
      WITH pl as pl

      MATCH (b:Units {id: {bId} })
        MERGE (b)-[rpl:UNITS_LIST]->(pl)

      WITH pl as pl

        UNWIND {Ids} as Id

          MATCH (p:Infos {id: Id})
            WITH p as cn, pl as pl
            SET cn :Data
            WITH pl as pl, cn as cn
              MERGE (pl)-[cnpt:DATA_LIST  { email: cn.email } ]->(cn)

          RETURN pl

Sample Data

List: 
{
   id: 'some-unique-id',
   name: "some-name''
}

Ids ( Ids should be around 100 ): 
  [ 'some-info-id-01','some-info-id-03' ]

Infos (Neo4j DB):
[
  { id: 'some-info-id-01', others: 'some-others-data-01' },
  { id: 'some-info-id-02', others: 'some-others-data-02' },
  { id: 'some-info-id-03', others: 'some-others-data-03' }
]

Any suggestion to improve this cypher query ??

PS, I'm running this CQL in my node.js app.

Upvotes: 0

Views: 59

Answers (1)

FrobberOfBits
FrobberOfBits

Reputation: 18002

This query looks like it should be pretty fast if you have proper indexes in place.

You should have these in place:

CREATE INDEX ON :Infos(id); CREATE INDEX ON :Units(id); CREATE INDEX ON :List(id);

With those indexes, the query should be fast because mostly you're looking up nodes by those IDs and then doing very small things on top of that. Even with 100 IDs that's not that hard of a query.

The counterpoint is that if you don't have your ID fields indexed, neo4j will have to look through most/all of them to figure out which items to match. The more data you have, the slower this query will get.

If you have these things indexed and you're still seeing very slow performance, you need to EXPLAIN the query and post the plan for further feedback.

Upvotes: 1

Related Questions