为什么我的Neo4j索引不适用?

时间:2016-06-10 18:57:12

标签: indexing neo4j cypher

我运行以下查询:

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

接下来,由于某种原因,当我尝试获取刚刚创建的Role时,不使用索引。你能解释一下原因吗?

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

1 个答案:

答案 0 :(得分:1)

您忘记使用索引。

试试这个:

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

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