Creating a RANGE Index on a Relationship Property

Performance improvement needed

In our Movie data model, we have identified another query as important and one that we need to make as fast as possible for our users. Here is an example where we are testing the rating property value for the RATED relationships in the graph.

cypher
PROFILE MATCH (u:User)-[r:RATED]->(m:Movie)
WHERE r.rating >= 4
RETURN u.name, r.rating, m.title

Step 1:

Profile this query and note if an index is used. Also note the number of total db hits and elapsed time. It does not use an index.

Step 2:

Create a RANGE index for the rating property of the RATED relationship type.

  • index_name: RATING_rating

  • relationship_type: RATED

  • property_key: rating

Step 3:

Repeat the query twice with a profile.

Why repeat when profiling?

You repeat the query because the first execution of the query includes time to parse the query, create the execution plan, and add it to the query cache. The first and second executions will have the same number of total db hits, but the difference will be the elapsed ms times. The second execution of the query is purely execution time of the query.

Was your newly-created RANGE index used?

Are the number of total db hits or elapsed time reduced?

Validate Results

Once you have completed the three steps of this Challenge, click the Check Indexes button and we will check the database for you.

Hint

Index names, label names, and property key names are all case-sensitive.

You can type SHOW INDEXES after you have created the index to confirm that it created the RANGE index.

If you mess up, you can reload this Challenge page and you should be where you need to be at the beginning of this challenge.

Then you would need to perform the steps of this Challenge again.

Solution

Here is the statement to create the RANGE index on the relationship property:

cypher
CREATE INDEX RATED_rating IF NOT EXISTS FOR ()-[x:RATED]-() ON (x.rating)

Summary

In this Challenge, you demonstrated that you can create a single property RANGE index on a relationship type in the graph that may or may not improve the performance of a query. In the next lesson, you will learn how to create Composite indexes.