APixel Visuals
APixel Visuals

Reputation: 1648

Neo4j - How do I decide what to index?

Let's say I've created 50K Persons:

UNWIND [ ... ] AS name
CREATE (:Person { name: name, joinTimestamp: 1000000 })

I need to have these nodes create random relationships between each other:

MATCH (p1:Person { joinTimestamp: 1000000 }), (p2:Person { joinTimestamp: 1000000 })
WITH p1, p2, rand() AS random
WHERE random < 0.001 AND p1 <> p2
MERGE (p1)-[:FRIENDS_WITH]->(p2)

This works, but I'd like to speed up that second query. What should I index? I would assume that Neo4j would use p1 and p2's ID to locate them during the MERGE. Should I index that? Or should I index the joinTimestamp property, since it's used in the MATCH query? Maybe indexing can't help this query at all?

Upvotes: 0

Views: 199

Answers (2)

cybersam
cybersam

Reputation: 66989

You cannot use the native ID of a node for indexing (since the native ID of a node is not really a "property" of the node). And you would not need to do that anyway, since neo4j can already quickly get to a node by its native ID.

Instead, you can index :Person(joinTimestamp), because joinTimestamp is a property of :Person (and your query can use it at the start).

By the way, you can always use profiling to verify that an index would be used.

Upvotes: 2

InverseFalcon
InverseFalcon

Reputation: 30397

Indexes in Neo4j are used to find starting points in the graph, you won't need them for traversals or things like that.

Since you're looking up these nodes by joinTimestamp, it makes sense to index :Person(joinTimestamp). Basically you'd look for the common means you plan to lookup your nodes in your queries (for example, looking up a person node by their name property) and add an index on those.

Upvotes: 0

Related Questions