On large graphs, performance becomes critical. Neo4j supports Index and tuning.

When to Use Indexes

  • You often MATCH nodes by a property (e.g., name, id).
  • These searches benefit greatly from an index.

How to Create an Index

CREATE INDEX person_name_index IF NOT EXISTS
FOR (p:Person)
ON (p.name)
  • Creates an index on the name property for Person nodes.
  • IF NOT EXISTS avoids errors if the index already exists.

Types of Indexes

TypeUse CaseExample
Property IndexSpeed up lookups by propertyCREATE INDEX FOR (p:Person) ON (p.name)
Composite IndexSearch by multiple propertiesCREATE INDEX FOR (p:Person) ON (p.name, p.age)
Full-text IndexSearch unstructured text fieldsMore advanced setup

Performance Tips

  • Always MATCH on indexed properties for faster queries.
  • Avoid Cartesian Products: if you forget to connect nodes in MATCH, Neo4j will combine everything (n * m) — extremely slow!
  • Limit the depth when matching variable-length paths (*1..3) to avoid exploring the entire graph.

Example of a bad Cartesian product:

MATCH (a:Person), (b:Person) 
RETURN a, b

(no relationship specified between a and b!)