Using Indexes in Neo4j

Posted By : Yasir Zuberi | 30-Nov-2018

Database index defines a data structure which improves data retrieval speed operations on a database table at the cost of additional writes and storage space to maintain the index data structure. 

Indices can be used for quickly locating data without having searching each row in database every time database table is accessed.

 

In Neo4j Cypher allows creation of indexes for one or more properties on all nodes given a label:

  • Single-property index - created on a single property for any given label 
  • Composite index - created on more than one property for any given label

After creating index, neo4j automatically manages indexes when ever graph changes. Neo4j starts using indexes right after their creation.


Create Single-property index 

Single property index can be created for all nodes which have particular label by using CREATE INDEX ON :Label(property)

CREATE INDEX ON :Book(title)
        

 

Create Composite index

Composite index can be created for all nodes which have particular label by using CREATE INDEX ON :Label(prop1, …?, propN)
It is to be noted that only those nodes with specified label and containing all properties defined in index creation will be added in index.

Below statement creates composite index for all nodes labeled with Book having both price and publisher property:

CREATE INDEX ON :Book(price, publisher)
        
//Lets take a look at below example

CREATE (a:Book {title: 'Harry Potter', price: 299, publisher: 'Readers'}), (b:Book {title: 'Narnia', price: 499}).
        

In above query, Node a has both price and publisher property, and so it will be added to the composite index. Whereas node b does not have publisher property and will not be added to composite index.

 

List all indexes created in neo4j database 

CALL db.indexes
        

 

Drop Single-property index 

Index on all nodes having a label and single property combination can be dropped by using DROP INDEX ON :Label(property).

DROP INDEX ON :Book(title)
        

 

Drop Composite index 

 

Composite index on all nodes having a label and multiple property combination can be dropped by using DROP INDEX ON :Label(prop1, …?, propN).

Below statement drops composite index on all nodes labeled with Book having both price and publisher property: 

DROP INDEX ON :Book(age, country)
        

 

Using Indexes in Neo4j

 

You don't have to explicitly specify indexes to be used in neo4j query. Cypher automatically figures out itself whether any index availble or not.

For example the query below will use the Book(title) index, if it exists. 

MATCH (book:Book { title: 'Avengers' })
RETURN book
        

 

Query Plan

 

Compiler CYPHER 3.5

Planner COST

Runtime INTERPRETED

Runtime version 3.5

+-----------------+----------------+------+---------+-----------------+-------------------+----------------------+----------------------+-----------+--------------------+
| Operator        | Estimated Rows | Rows | DB Hits | Page Cache Hits | Page Cache Misses | Page Cache Hit Ratio | Order                | Variables | Other              |
+-----------------+----------------+------+---------+-----------------+-------------------+----------------------+----------------------+-----------+--------------------+
| +ProduceResults |              1 |    1 |       0 |               2 |                 1 |               0.6667 | book.title ASC | book    |                    |
| |               +----------------+------+---------+-----------------+-------------------+----------------------+----------------------+-----------+--------------------+
| +NodeIndexSeek  |              1 |    1 |       3 |               2 |                 1 |               0.6667 | book.title ASC | book    | :Book(title) |
+-----------------+----------------+------+---------+-----------------+-------------------+----------------------+----------------------+-----------+--------------------+

Total database accesses: 3
        

About Author

Author Image
Yasir Zuberi

Yasir is Lead Developer. He is a bright Java and Grails developer and have worked on development of various SaaS applications using Grails framework.

Request for Proposal

Name is required

Comment is required

Sending message..