Neo4j Efficiently Query Multiple Nodes Each With Different WHERE Values

Given a database with thousands of nodes:

  • MERGE (n1:Node {id:1});
  • MERGE (n2:Node {id:2, first:"John", last:"Doe"});
  • etc

I want to construct a SCALABLE query that returns nodes if any 1 of the following conditions applies:

  • (node.id = 12) OR (node.first = "Turkey" AND node.last = "Legs")
  • (node.id = 12) OR (node.first = "John" AND node.last = "Doe")
  • (node.id = 1) OR (node.first = "Jiggly" AND node.last = "Puff")
  • thousands more

I have come up with the following query but it threw an out-of-memory error when the number of conditions reached 10,000

WITH [
   {id: 12, first: "Turkey", last: "Legs"},
   {id: 12, first: "John", last: "Doe"},
   {id: 1, first: "Jiggly", last: "Puff"}
   // thousands more
] AS conditions
UNWIND conditions AS condition
MATCH (n:Node) WHERE node.id = condition.id) AND (n.first = condition.first AND n.last = condition.last)
RETURN collect(DISTINCT n)

Just in case you are wondering, I have added indices and composite indices where needed