cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Faster alternatives to `Index + Params + Where IN` for node lookup?

We are using the follow Cypher query:

MATCH (n:Entity)-[r]->(m) WHERE n.cid IN $cids RETURN labels(n), n, type(r), m"
  • There is a UNIQUE INDEX CONSTRAINT on Entity.cid
  • $cids is a param which is a list of cids (in the 1-500 range on average)

Are there any other query optimisations we can try to speed up this query?

1 ACCEPTED SOLUTION

MuddyBootsCode
Graph Steward

If you've already got the list of cids, then you can simply call the matches:

UNWIND $cids AS cid
MATCH(n:Entity {id: cid})-[r]->(m)
return labels(n), type(r), m

should be quite a bit faster.

View solution in original post

3 REPLIES 3

MuddyBootsCode
Graph Steward

If you've already got the list of cids, then you can simply call the matches:

UNWIND $cids AS cid
MATCH(n:Entity {id: cid})-[r]->(m)
return labels(n), type(r), m

should be quite a bit faster.

Awesome! For some reason I didn't think UNWIND would improve performance much for this instance, but it scales much better like this. For 500 cids in the list it is almost 10x faster to the initial query.

Thanks a lot!

EDIT: Silly me forgot to change Muddy's query and replace {id: cid} with the correct {cid: cid} so the query is still a bit faster but more like 1.5x instead of 10x - because it didn't fetch anything when it had the typo.

Yep, calling the exact node is always faster. Glad I could help.

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

All the sessions of the conference are now available online