Stephen D
Stephen D

Reputation: 3076

Why isn't my Neo4j index applying?

I run the following queries:

CREATE INDEX ON :Role(id)
+-------------------+
| No data returned. |
+-------------------+
Indexes added: 1


CREATE (:Role {id:'abc'})
+-------------------+
| No data returned. |
+-------------------+
Nodes created: 1
Properties set: 1
Labels added: 1

And next, for some reason, the index is not used when I try to fetch the Role that I just created. Can you explain why?

neo4j-sh (?)$ PROFILE MATCH (role:Role {id:'abc'})
> RETURN role.id;
+---------+
| role.id |
+---------+
| "abc"   |
+---------+
1 row
7 ms

Compiler CYPHER 2.2

Planner COST

Projection
  |
  +Filter
    |
    +NodeByLabelScan

+-----------------+---------------+------+--------+---------------+----------------------------+
|        Operator | EstimatedRows | Rows | DbHits |   Identifiers |                      Other |
+-----------------+---------------+------+--------+---------------+----------------------------+
|      Projection |             1 |    1 |      1 | role, role.id |                    role.id |
|          Filter |             1 |    1 |      5 |          role | role.id == {  AUTOSTRING0} |
| NodeByLabelScan |             6 |    5 |      6 |          role |                      :Role |
+-----------------+---------------+------+--------+---------------+----------------------------+

Total database accesses: 12

Upvotes: 2

Views: 45

Answers (1)

Matt Dubois
Matt Dubois

Reputation: 26

You forgot to use the index.

Try this:

MATCH (role:Role)
USING INDEX role:Role(id)
WHERE role.id='abc'
RETURN role.id

Projection
  |
  +Expand(All)
    |
    +NodeIndexSeek
Total database accesses: 4

Upvotes: 1

Related Questions