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?

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.

2 Likes

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.