Course contentsShow
← System Design
Lesson 469 of 1,91913. NoSQL: Graph DatabasesFree lesson

Indexes and Query Performance

Creating indexes on node labels and properties, using EXPLAIN and PROFILE to optimize traversal queries.

Indexes and Query Performance

You'll learn how to speed up graph queries by creating indexes on frequently searched properties and using tools to analyze query execution.

Why Indexes Matter in Graph Databases

Even with index-free adjacency for traversing relationships, you still need a starting point for your queries. Without indexes, finding that first node means scanning potentially millions of nodes—like searching every house in a city to find one address.

Indexes create shortcuts. When you frequently search for users by email or products by SKU, an index lets the database jump directly to matching nodes instead of checking each one.

Creating Indexes in Neo4j

CREATE INDEX FOR (u:User) ON (u.email);
CREATE INDEX FOR (p:Product) ON (p.sku);

These indexes optimize queries that filter by those properties:

MATCH (u:User {email: 'alice@example.com'})
RETURN u;

Without the index, Neo4j scans all User nodes. With it, lookup is near-instant.

Analyzing Performance

EXPLAIN shows the query plan without running it:

EXPLAIN MATCH (u:User {email: 'bob@example.com'})
        -[:FRIENDS_WITH*2..3]->(friend)
RETURN friend;

This reveals whether indexes are used and how the traversal will execute.

PROFILE runs the query and shows performance metrics:

PROFILE MATCH (u:User {email: 'bob@example.com'})
        -[:FRIENDS_WITH*2..3]->(friend)
RETURN friend;

It displays how many database hits occurred and how many rows were processed at each step—crucial for identifying bottlenecks in complex traversals.

Key Takeaway: Index node labels and frequently queried properties to speed up query entry points, and use EXPLAIN/PROFILE to understand and optimize how graph traversals execute under the hood.