Introduction
In the following lessons you will learn some of the common best practices for writing efficient Cypher queries including:
-
Use of indexes
-
Anchor nodes
-
Count stores
-
Using WITH to break down complex queries
-
Reading properties late
Query Optimization Checklist
When analyzing slow queries, check for these common issues:
-
Node labels are specified - Use
MATCH (u:User)instead ofMATCH (u)to avoid scanning all nodes -
Properties have indexes - Create indexes on properties used in
WHEREclauses and lookups -
Relationship types are specified - Always provide relationship types to narrow traversals
-
Relationship directions are specified - Always specify the direction of the relationship when it is known
-
Queries use parameters - Replace literal values with
$parameterto enable plan caching -
Return only needed properties - Use
RETURN u.name, m.titleinstead ofRETURN u, mto reduce data transfer -
Result sets are limited - Add
LIMITclauses to prevent returning excessive data -
Variable length paths have upper limits - Use
[*1..5]instead of[*]to prevent excessive traversals
Lesson Summary
In this lesson, you learned …
In the next lesson, you will …