Graph Indexes and Performance
You'll learn how to speed up graph queries by creating indexes on node labels and properties.
Why Indexes Matter in Graph Databases
As your graph grows to millions of nodes and relationships, finding specific nodes can become painfully slow. Without indexes, the database must scan every node to find matches—like searching every house in a city to find one address.
Graph databases let you create indexes on node labels and properties to dramatically speed up lookups. When you index a property, the database builds an optimized lookup structure (often using B-Trees or hash indexes) that maps property values directly to nodes.
How Graph Indexes Work
Think of an index as a phone book. Without it, you'd read every entry to find "John Smith." With it, you jump straight to the S section.
When you execute a query like MATCH (u:User {email: 'alice@example.com'}), the database:
- Without index: Scans all User nodes checking each email property (O(n))
- With index on User.email: Uses the index to find the node instantly (O(log n) or O(1))
Creating Indexes
Most graph databases support simple index creation commands. You specify:
- The node label (e.g., User, Product)
- The property to index (e.g., email, productId)
The index is built in the background and used automatically by the query planner when appropriate.
When to Use Indexes
Index properties you frequently filter on or use for lookups:
- Unique identifiers (user IDs, email addresses)
- Properties in WHERE clauses
- Properties used to start traversals
Avoid over-indexing: each index consumes memory and slows down writes.
Key Takeaway: Creating indexes on frequently-queried node labels and properties transforms slow full-table scans into fast lookups, making large-scale graph traversal practical.