13.1. Indexes

A database index is a redundant copy of information in the database for the purpose of making retrieving said data more efficient. This comes at the cost of additional storage costs and slower write performance.

Cypher allows the creation of indexes over a property for all nodes that have a given label. These indexes are automatically managed and kept up to date by the database whenever the graph is changed.

Create index on a label

To create an index on a property for all nodes that have a label, use CREATE INDEX ON. Note that the index is not immediately available, but will be created in the background. See the section called “Indexes” for details.

Query 

CREATE INDEX ON :Person(name)

Result

(empty result)

Try this query live create index on :`Person`(`name`) create (_0:`Person` {`name`:"Andres"}) create (_1:`Person` {`name`:"Mark"}) create _0-[:`KNOWS`]->_1 create index on :Person(name)

Drop index on a label

To drop an index on all nodes that have a label, use the DROP INDEX clause.

Query 

DROP INDEX ON :Person(name)

Result

Indexes removed: 1

(empty result)

Try this query live create index on :`Person`(`name`) create (_0:`Person` {`name`:"Andres"}) create (_1:`Person` {`name`:"Mark"}) create _0-[:`KNOWS`]->_1 drop index on :Person(name)

Use index

There is usually no need to specify which indexes to use in a query, Cypher will figure that out by itself. For example the query below will use the Person(name) index, if it exists. If you for some reason want to hint to specific indexes, see Section 9.8, “Using”.

Query 

MATCH (person:Person { name: 'Andres' })
RETURN person

Result

person
1 row

Node[0]{name:"Andres"}

Try this query live create index on :`Person`(`name`) create (_0:`Person` {`name`:"Andres"}) create (_1:`Person` {`name`:"Mark"}) create _0-[:`KNOWS`]->_1 match (person:Person {name: 'Andres'}) return person

Use index with WHERE

Indexes are also automatically used for equality comparisons of a indexed property in the WHERE clause.If you for some reason want to hint to specific indexes, see Section 9.8, “Using”.

Query 

MATCH (person:Person)
WHERE person.name = 'Andres'
RETURN person

Result

person
1 row

Node[0]{name:"Andres"}

Try this query live create index on :`Person`(`name`) create (_0:`Person` {`name`:"Andres"}) create (_1:`Person` {`name`:"Mark"}) create _0-[:`KNOWS`]->_1 match (person:Person) WHERE person.name = 'Andres' return person

Use index with IN

The IN predicate on person.name in the following query will use the Person(name) index, if it exists. If you for some reason want Cypher to use specific indexes, you can enforce it using hints. See Section 9.8, “Using”.

Query 

MATCH (person:Person)
WHERE person.name IN ['Andres', 'Mark']
RETURN person

Result

person
2 rows

Node[0]{name:"Andres"}

Node[1]{name:"Mark"}

Try this query live create index on :`Person`(`name`) create (_0:`Person` {`name`:"Andres"}) create (_1:`Person` {`name`:"Mark"}) create _0-[:`KNOWS`]->_1 match (person:Person) WHERE person.name IN ['Andres','Mark'] return person