The use of indexes
The task of tuning calls for different indexes depending on what the queries look like. Therefore, it is important to have a fundamental understanding of how the indexes operate. This section describes the query plans that result from different index scenarios.
Please refer to Indexes for search performance for instructions on how to create and maintain the indexes themselves.
A simple example
In the example below, the query will use a Person(firstname)
index, if it exists.
MATCH (person:Person {firstname: 'Andy'}) RETURN person
Compiler CYPHER 4.0
Planner COST
Runtime PIPELINED
Runtime version 4.0
+-----------------+----------------+------+---------+----------------------+-----------+--------------------+
| Operator | Estimated Rows | Rows | DB Hits | Order | Variables | Other |
+-----------------+----------------+------+---------+----------------------+-----------+--------------------+
| +ProduceResults | 1 | 1 | 0 | person.firstname ASC | person | |
| | +----------------+------+---------+----------------------+-----------+--------------------+
| +NodeIndexSeek | 1 | 1 | 2 | person.firstname ASC | person | :Person(firstname) |
+-----------------+----------------+------+---------+----------------------+-----------+--------------------+
Total database accesses: 2
Equality check using WHERE
(single-property index)
A query containing equality comparisons of a single indexed property in the WHERE
clause is backed automatically by the index. It is also possible for a query with multiple OR
predicates to use multiple indexes, if indexes exist on the properties. For example, if indexes exist on both :Label(p1)
and :Label(p2)
, MATCH (n:Label) WHERE n.p1 = 1 OR n.p2 = 2 RETURN n
will use both indexes.
MATCH (person:Person) WHERE person.firstname = 'Andy' RETURN person
Compiler CYPHER 4.0
Planner COST
Runtime PIPELINED
Runtime version 4.0
+-----------------+----------------+------+---------+----------------------+-----------+--------------------+
| Operator | Estimated Rows | Rows | DB Hits | Order | Variables | Other |
+-----------------+----------------+------+---------+----------------------+-----------+--------------------+
| +ProduceResults | 1 | 1 | 0 | person.firstname ASC | person | |
| | +----------------+------+---------+----------------------+-----------+--------------------+
| +NodeIndexSeek | 1 | 1 | 2 | person.firstname ASC | person | :Person(firstname) |
+-----------------+----------------+------+---------+----------------------+-----------+--------------------+
Total database accesses: 2
Equality check using WHERE
(composite index)
A query containing equality comparisons for all the properties of a composite index will automatically be backed by the same index. However, the query does not need to have equality on all properties. It can have ranges and existence predicates as well. But in these cases rewrites might happen depending on which properties have which predicates, see composite index limitations. The following query will use the composite index defined earlier:
MATCH (n:Person) WHERE n.age = 35 AND n.country = 'UK' RETURN n
However, the query MATCH (n:Person) WHERE n.age = 35 RETURN n
will not be backed by the composite index, as the query does not contain a predicate on the country
property. It will only be backed by an index on the Person
label and age
property defined thus: :Person(age)
; i.e. a single-property index.
+-------------------------------------------------------------------------------------------+
| n |
+-------------------------------------------------------------------------------------------+
| Node[0]{country:"UK",highScore:54321,firstname:"John",surname:"Smith",name:"john",age:35} |
+-------------------------------------------------------------------------------------------+
1 row
Range comparisons using WHERE
(single-property index)
Single-property indexes are also automatically used for inequality (range) comparisons of an indexed property in the WHERE
clause.
MATCH (person:Person) WHERE person.firstname > 'B' RETURN person
Compiler CYPHER 4.0
Planner COST
Runtime PIPELINED
Runtime version 4.0
+-----------------------+----------------+------+---------+----------------------+-----------+-------------------------------------+
| Operator | Estimated Rows | Rows | DB Hits | Order | Variables | Other |
+-----------------------+----------------+------+---------+----------------------+-----------+-------------------------------------+
| +ProduceResults | 1 | 1 | 0 | person.firstname ASC | person | |
| | +----------------+------+---------+----------------------+-----------+-------------------------------------+
| +NodeIndexSeekByRange | 1 | 1 | 2 | person.firstname ASC | person | :Person(firstname) > $ AUTOSTRING0 |
+-----------------------+----------------+------+---------+----------------------+-----------+-------------------------------------+
Total database accesses: 2
Range comparisons using WHERE
(composite index)
Composite indexes are also automatically used for inequality (range) comparisons of indexed properties in the WHERE
clause. Equality or list membership check predicates may precede the range predicate. However, predicates after the range predicate may be rewritten as an existence check predicate and a filter as described in composite index limitations.
MATCH (person:Person) WHERE person.firstname > 'B' AND person.highScore > 10000 RETURN person
Compiler CYPHER 4.0
Planner COST
Runtime PIPELINED
Runtime version 4.0
+------------------------------+----------------+------+---------+-----------+--------------------------------------------+-----------+-------------------------------------------------------+
| Operator | Estimated Rows | Rows | DB Hits | Time (ms) | Order | Variables | Other |
+------------------------------+----------------+------+---------+-----------+--------------------------------------------+-----------+-------------------------------------------------------+
| +ProduceResults | 0 | 1 | 0 | 0.102 | person.firstname ASC, person.highScore ASC | person | |
| | +----------------+------+---------+-----------+--------------------------------------------+-----------+-------------------------------------------------------+
| +Filter | 0 | 1 | 0 | 0.075 | person.firstname ASC, person.highScore ASC | person | cache[person.highScore] > $` AUTOINT1` |
| | +----------------+------+---------+-----------+--------------------------------------------+-----------+-------------------------------------------------------+
| +NodeIndexSeek(range,exists) | 0 | 1 | 2 | 1.400 | person.firstname ASC, person.highScore ASC | person | :Person(firstname,highScore), cache[person.highScore] |
+------------------------------+----------------+------+---------+-----------+--------------------------------------------+-----------+-------------------------------------------------------+
Total database accesses: 2
Multiple range comparisons using WHERE
(single-property index)
When the WHERE
clause contains multiple inequality (range) comparisons for the same property, these can be combined in a single index range seek.
MATCH (person:Person) WHERE 10000 < person.highScore < 20000 RETURN person
Compiler CYPHER 4.0
Planner COST
Runtime PIPELINED
Runtime version 4.0
+-----------------------+----------------+------+---------+----------------------+-----------+-----------------------------------------------------------------------+
| Operator | Estimated Rows | Rows | DB Hits | Order | Variables | Other |
+-----------------------+----------------+------+---------+----------------------+-----------+-----------------------------------------------------------------------+
| +ProduceResults | 1 | 1 | 0 | person.highScore ASC | person | |
| | +----------------+------+---------+----------------------+-----------+-----------------------------------------------------------------------+
| +NodeIndexSeekByRange | 1 | 1 | 2 | person.highScore ASC | person | :Person(highScore) > $ AUTOINT1 AND :Person(highScore) < $ AUTOINT0 |
+-----------------------+----------------+------+---------+----------------------+-----------+-----------------------------------------------------------------------+
Total database accesses: 2
Multiple range comparisons using WHERE
(composite index)
When the WHERE
clause contains multiple inequality (range) comparisons for the same property, these can be combined in a single index range seek. That single range seek created in the following query will then use the composite index Person(highScore, name)
if it exists.
MATCH (person:Person) WHERE 10000 < person.highScore < 20000 AND exists(person.name) RETURN person
Compiler CYPHER 4.0
Planner COST
Runtime PIPELINED
Runtime version 4.0
+------------------------------+----------------+------+---------+-----------+-----------+-------------------------+
| Operator | Estimated Rows | Rows | DB Hits | Time (ms) | Variables | Other |
+------------------------------+----------------+------+---------+-----------+-----------+-------------------------+
| +ProduceResults | 1 | 1 | 0 | 0.133 | person | |
| | +----------------+------+---------+-----------+-----------+-------------------------+
| +NodeIndexSeek(range,exists) | 1 | 1 | 2 | 6.807 | person | :Person(highScore,name) |
+------------------------------+----------------+------+---------+-----------+-----------+-------------------------+
Total database accesses: 2
List membership check using IN
(single-property index)
The IN
predicate on person.firstname
in the following query will use the single-property index Person(firstname)
if it exists.
MATCH (person:Person) WHERE person.firstname IN ['Andy', 'John'] RETURN person
Compiler CYPHER 4.0
Planner COST
Runtime PIPELINED
Runtime version 4.0
+-----------------+----------------+------+---------+-----------+--------------------+
| Operator | Estimated Rows | Rows | DB Hits | Variables | Other |
+-----------------+----------------+------+---------+-----------+--------------------+
| +ProduceResults | 24 | 2 | 0 | person | |
| | +----------------+------+---------+-----------+--------------------+
| +NodeIndexSeek | 24 | 2 | 0 | person | :Person(firstname) |
+-----------------+----------------+------+---------+-----------+--------------------+
Total database accesses: 0
List membership check using IN
(composite index)
The IN
predicates on person.age
and person.country
in the following query will use the composite index Person(age, country)
if it exists.
MATCH (person:Person) WHERE person.age IN [10, 20, 35] AND person.country IN ['Sweden', 'USA',
'UK'] RETURN person
Compiler CYPHER 4.0
Planner COST
Runtime PIPELINED
Runtime version 4.0
+-----------------------------------+----------------+------+---------+-----------+-----------+----------------------+
| Operator | Estimated Rows | Rows | DB Hits | Time (ms) | Variables | Other |
+-----------------------------------+----------------+------+---------+-----------+-----------+----------------------+
| +ProduceResults | 451 | 1 | 0 | 0.120 | person | |
| | +----------------+------+---------+-----------+-----------+----------------------+
| +NodeIndexSeek(equality,equality) | 451 | 1 | 10 | 6.553 | person | :Person(age,country) |
+-----------------------------------+----------------+------+---------+-----------+-----------+----------------------+
Total database accesses: 10
Prefix search using STARTS WITH
(single-property index)
The STARTS WITH
predicate on person.firstname
in the following query will use the Person(firstname)
index, if it exists.
MATCH (person:Person) WHERE person.firstname STARTS WITH 'And' RETURN person
Compiler CYPHER 4.0
Planner COST
Runtime PIPELINED
Runtime version 4.0
+-----------------------+----------------+------+---------+-----------+----------------------+-----------+-------------------------------------------------+
| Operator | Estimated Rows | Rows | DB Hits | Time (ms) | Order | Variables | Other |
+-----------------------+----------------+------+---------+-----------+----------------------+-----------+-------------------------------------------------+
| +ProduceResults | 2 | 1 | 0 | 0.106 | person.firstname ASC | person | |
| | +----------------+------+---------+-----------+----------------------+-----------+-------------------------------------------------+
| +NodeIndexSeekByRange | 2 | 1 | 2 | 0.206 | person.firstname ASC | person | :Person(firstname STARTS WITH $` AUTOSTRING0`) |
+-----------------------+----------------+------+---------+-----------+----------------------+-----------+-------------------------------------------------+
Total database accesses: 2
Prefix search using STARTS WITH
(composite index)
The STARTS WITH
predicate on person.firstname
in the following query will use the Person(firstname,surname)
index, if it exists. Any (non-existence check) predicate on person.surname
will be rewritten as existence check with a filter. However, if the predicate on person.firstname
is a equality check then a STARTS WITH
on person.surname
would also use the index (without rewrites). More information about how the rewriting works can be found in composite index limitations.
MATCH (person:Person) WHERE person.firstname STARTS WITH 'And' AND exists(person.surname) RETURN
person
Compiler CYPHER 4.0
Planner COST
Runtime PIPELINED
Runtime version 4.0
+------------------------------+----------------+------+---------+-----------+-----------+----------------------------+
| Operator | Estimated Rows | Rows | DB Hits | Time (ms) | Variables | Other |
+------------------------------+----------------+------+---------+-----------+-----------+----------------------------+
| +ProduceResults | 1 | 1 | 0 | 0.100 | person | |
| | +----------------+------+---------+-----------+-----------+----------------------------+
| +NodeIndexSeek(range,exists) | 1 | 1 | 2 | 0.257 | person | :Person(firstname,surname) |
+------------------------------+----------------+------+---------+-----------+-----------+----------------------------+
Total database accesses: 2
Suffix search using ENDS WITH
(single-property index)
The ENDS WITH
predicate on person.firstname
in the following query will use the Person(firstname)
index, if it exists. All values stored in the Person(firstname)
index will be searched, and entries ending with 'hn'
will be returned. This means that although the search will not be optimized to the extent of queries using =
, IN
, >
, <
or STARTS WITH
, it is still faster than not using an index in the first place. Composite indexes are currently not able to support ENDS WITH
.
MATCH (person:Person) WHERE person.firstname ENDS WITH 'hn' RETURN person
Compiler CYPHER 4.0
Planner COST
Runtime PIPELINED
Runtime version 4.0
+------------------------+----------------+------+---------+----------------------+-----------+--------------------------------------+
| Operator | Estimated Rows | Rows | DB Hits | Order | Variables | Other |
+------------------------+----------------+------+---------+----------------------+-----------+--------------------------------------+
| +ProduceResults | 2 | 1 | 0 | person.firstname ASC | person | |
| | +----------------+------+---------+----------------------+-----------+--------------------------------------+
| +NodeIndexEndsWithScan | 2 | 1 | 2 | person.firstname ASC | person | :Person(firstname); $` AUTOSTRING0` |
+------------------------+----------------+------+---------+----------------------+-----------+--------------------------------------+
Total database accesses: 2
Suffix search using ENDS WITH
(composite index)
The ENDS WITH
predicate on person.surname
in the following query will use the Person(surname,age)
index, if it exists. However, it will be rewritten as existence check and a filter due to the index not supporting actual suffix searches for composite indexes, this is still faster than not using an index in the first place. Any (non-existence check) predicate on person.age
will also be rewritten as existence check with a filter. More information about how the rewriting works can be found in composite index limitations.
MATCH (person:Person) WHERE person.surname ENDS WITH '300' AND exists(person.age) RETURN person
Compiler CYPHER 4.0
Planner COST
Runtime PIPELINED
Runtime version 4.0
+-----------------+----------------+------+---------+-----------+--------------------------------------------------+
| Operator | Estimated Rows | Rows | DB Hits | Variables | Other |
+-----------------+----------------+------+---------+-----------+--------------------------------------------------+
| +ProduceResults | 11 | 1 | 0 | person | |
| | +----------------+------+---------+-----------+--------------------------------------------------+
| +Filter | 11 | 1 | 0 | person | cache[person.surname] ENDS WITH $` AUTOSTRING0` |
| | +----------------+------+---------+-----------+--------------------------------------------------+
| +NodeIndexScan | 106 | 303 | 304 | person | :Person(surname,age), cache[person.surname] |
+-----------------+----------------+------+---------+-----------+--------------------------------------------------+
Total database accesses: 304
Substring search using CONTAINS
(single-property index)
The CONTAINS
predicate on person.firstname
in the following query will use the Person(firstname)
index, if it exists. All values stored in the Person(firstname)
index will be searched, and entries containing 'h'
will be returned. This means that although the search will not be optimized to the extent of queries using =
, IN
, >
, <
or STARTS WITH
, it is still faster than not using an index in the first place. Composite indexes are currently not able to support CONTAINS
.
MATCH (person:Person) WHERE person.firstname CONTAINS 'h' RETURN person
Compiler CYPHER 4.0
Planner COST
Runtime PIPELINED
Runtime version 4.0
+------------------------+----------------+------+---------+----------------------+-----------+--------------------------------------+
| Operator | Estimated Rows | Rows | DB Hits | Order | Variables | Other |
+------------------------+----------------+------+---------+----------------------+-----------+--------------------------------------+
| +ProduceResults | 2 | 1 | 0 | person.firstname ASC | person | |
| | +----------------+------+---------+----------------------+-----------+--------------------------------------+
| +NodeIndexContainsScan | 2 | 1 | 2 | person.firstname ASC | person | :Person(firstname); $` AUTOSTRING0` |
+------------------------+----------------+------+---------+----------------------+-----------+--------------------------------------+
Total database accesses: 2
Substring search using CONTAINS
(composite index)
The CONTAINS
predicate on person.surname
in the following query will use the Person(surname,age)
index, if it exists. However, it will be rewritten as existence check and a filter due to the index not supporting actual suffix searches for composite indexes, this is still faster than not using an index in the first place. Any (non-existence check) predicate on person.age
will also be rewritten as existence check with a filter. More information about how the rewriting works can be found in composite index limitations.
MATCH (person:Person) WHERE person.surname CONTAINS '300' AND exists(person.age) RETURN person
Compiler CYPHER 4.0
Planner COST
Runtime PIPELINED
Runtime version 4.0
+-----------------+----------------+------+---------+-----------+-------------------------------------------------+
| Operator | Estimated Rows | Rows | DB Hits | Variables | Other |
+-----------------+----------------+------+---------+-----------+-------------------------------------------------+
| +ProduceResults | 11 | 1 | 0 | person | |
| | +----------------+------+---------+-----------+-------------------------------------------------+
| +Filter | 11 | 1 | 0 | person | cache[person.surname] CONTAINS $` AUTOSTRING0` |
| | +----------------+------+---------+-----------+-------------------------------------------------+
| +NodeIndexScan | 106 | 303 | 304 | person | :Person(surname,age), cache[person.surname] |
+-----------------+----------------+------+---------+-----------+-------------------------------------------------+
Total database accesses: 304
Existence check using exists
(single-property index)
The exists(p.firstname)
predicate in the following query will use the Person(firstname)
index, if it exists.
MATCH (p:Person) WHERE exists(p.firstname) RETURN p
Compiler CYPHER 4.0
Planner COST
Runtime PIPELINED
Runtime version 4.0
+-----------------+----------------+------+---------+-----------+--------------------+
| Operator | Estimated Rows | Rows | DB Hits | Variables | Other |
+-----------------+----------------+------+---------+-----------+--------------------+
| +ProduceResults | 2 | 2 | 0 | p | |
| | +----------------+------+---------+-----------+--------------------+
| +NodeIndexScan | 2 | 2 | 3 | p | :Person(firstname) |
+-----------------+----------------+------+---------+-----------+--------------------+
Total database accesses: 3
Existence check using exists
(composite index)
The exists(p.firstname)
and exists(p.surname)
predicate in the following query will use the Person(firstname,surname)
index, if it exists. Any (non-existence check) predicate on person.surname
will be rewritten as existence check with a filter.
MATCH (p:Person) WHERE exists(p.firstname) AND exists(p.surname) RETURN p
Compiler CYPHER 4.0
Planner COST
Runtime PIPELINED
Runtime version 4.0
+-----------------+----------------+------+---------+-----------+----------------------------+
| Operator | Estimated Rows | Rows | DB Hits | Variables | Other |
+-----------------+----------------+------+---------+-----------+----------------------------+
| +ProduceResults | 1 | 2 | 0 | p | |
| | +----------------+------+---------+-----------+----------------------------+
| +NodeIndexScan | 1 | 2 | 3 | p | :Person(firstname,surname) |
+-----------------+----------------+------+---------+-----------+----------------------------+
Total database accesses: 3
Spatial distance searches (single-property index)
If a property with point values is indexed, the index is used for spatial distance searches as well as for range queries.
MATCH (p:Person) WHERE distance(p.location, point({x: 1, y: 2})) < 2 RETURN p.location
Compiler CYPHER 4.0
Planner COST
Runtime PIPELINED
Runtime version 4.0
+-----------------------+----------------+------+---------+-----------+-----------------+---------------------------------------------------------------------------------------------------+
| Operator | Estimated Rows | Rows | DB Hits | Time (ms) | Variables | Other |
+-----------------------+----------------+------+---------+-----------+-----------------+---------------------------------------------------------------------------------------------------+
| +ProduceResults | 0 | 9 | 0 | 0.146 | p, p.location | |
| | +----------------+------+---------+-----------+-----------------+---------------------------------------------------------------------------------------------------+
| +Projection | 0 | 9 | 0 | 0.012 | p.location -- p | {p.location : cache[p.location]} |
| | +----------------+------+---------+-----------+-----------------+---------------------------------------------------------------------------------------------------+
| +Filter | 0 | 9 | 0 | 0.130 | p | distance(cache[p.location], point({x: $` AUTOINT0`, y: $` AUTOINT1`})) < $` AUTOINT2` |
| | +----------------+------+---------+-----------+-----------------+---------------------------------------------------------------------------------------------------+
| +NodeIndexSeekByRange | 0 | 9 | 10 | 0.589 | p | :Person(location) WHERE distance(_,point(x,y)) < Parameter( AUTOINT2,Integer), cache[p.location] |
+-----------------------+----------------+------+---------+-----------+-----------------+---------------------------------------------------------------------------------------------------+
Total database accesses: 10
Spatial distance searches (composite index)
If a property with point values is indexed, the index is used for spatial distance searches as well as for range queries. Any following (non-existence check) predicates (here on property p.name
for index :Person(place,name)
) will be rewritten as existence check with a filter.
MATCH (p:Person) WHERE distance(p.place, point({x: 1, y: 2})) < 2 AND exists(p.name) RETURN p.place
Compiler CYPHER 4.0
Planner COST
Runtime PIPELINED
Runtime version 4.0
+------------------------------+----------------+------+---------+-----------+--------------+---------------------------------------------------------------------------------------+
| Operator | Estimated Rows | Rows | DB Hits | Time (ms) | Variables | Other |
+------------------------------+----------------+------+---------+-----------+--------------+---------------------------------------------------------------------------------------+
| +ProduceResults | 69 | 9 | 0 | 0.319 | p, p.place | |
| | +----------------+------+---------+-----------+--------------+---------------------------------------------------------------------------------------+
| +Projection | 69 | 9 | 0 | 0.017 | p.place -- p | {p.place : cache[p.place]} |
| | +----------------+------+---------+-----------+--------------+---------------------------------------------------------------------------------------+
| +Filter | 69 | 9 | 0 | 0.354 | p | distance(cache[p.place], point({x: $` AUTOINT0`, y: $` AUTOINT1`})) < $` AUTOINT2` |
| | +----------------+------+---------+-----------+--------------+---------------------------------------------------------------------------------------+
| +NodeIndexSeek(range,exists) | 69 | 9 | 10 | 2.846 | p | :Person(place,name), cache[p.place] |
+------------------------------+----------------+------+---------+-----------+--------------+---------------------------------------------------------------------------------------+
Total database accesses: 10
Spatial bounding box searches (single-property index)
The ability to do index seeks on bounded ranges works even with the 2D and 3D spatial Point
types.
MATCH (person:Person) WHERE point({x: 1, y: 5}) < person.location < point({x: 2, y: 6}) RETURN person
Compiler CYPHER 4.0
Planner COST
Runtime PIPELINED
Runtime version 4.0
+-----------------------+----------------+------+---------+-----------+-----------------------------------------------------------------------------------------------------------------------------+
| Operator | Estimated Rows | Rows | DB Hits | Variables | Other |
+-----------------------+----------------+------+---------+-----------+-----------------------------------------------------------------------------------------------------------------------------+
| +ProduceResults | 0 | 1 | 0 | person | |
| | +----------------+------+---------+-----------+-----------------------------------------------------------------------------------------------------------------------------+
| +NodeIndexSeekByRange | 0 | 1 | 2 | person | :Person(location) > point({x: $ AUTOINT2, y: $ AUTOINT3}) AND :Person(location) < point({x: $ AUTOINT0, y: $ AUTOINT1}) |
+-----------------------+----------------+------+---------+-----------+-----------------------------------------------------------------------------------------------------------------------------+
Total database accesses: 2
Spatial bounding box searches (composite index)
The ability to do index seeks on bounded ranges works even with the 2D and 3D spatial Point
types. Any following (non-existence check) predicates (here on property p.firstname
for index :Person(place,firstname)
) will be rewritten as existence check with a filter. For index :Person(firstname,place)
, if the predicate on firstname
is equality or list membership then the bounded range is handled as a range itself. If the predicate on firstname
is anything else then the bounded range is rewritten to existence and filter.
MATCH (person:Person) WHERE point({x: 1, y: 5}) < person.place < point({x: 2,
y: 6}) AND exists(person.firstname) RETURN person
Compiler CYPHER 4.0
Planner COST
Runtime PIPELINED
Runtime version 4.0
+------------------------------+----------------+------+---------+-----------+-----------+--------------------------+
| Operator | Estimated Rows | Rows | DB Hits | Time (ms) | Variables | Other |
+------------------------------+----------------+------+---------+-----------+-----------+--------------------------+
| +ProduceResults | 0 | 1 | 0 | 0.151 | person | |
| | +----------------+------+---------+-----------+-----------+--------------------------+
| +NodeIndexSeek(range,exists) | 0 | 1 | 2 | 0.765 | person | :Person(place,firstname) |
+------------------------------+----------------+------+---------+-----------+-----------+--------------------------+
Total database accesses: 2