Introduction
In this challenge you will be presented with a query that exhibits common performance issues.
You should:
-
Use
PROFILEto analyze the query. -
Identify the root causes of the performance issues.
-
Implement effective solutions to resolve the performance problems.
Finding product reorder levels by category
The following query finds the reorder levels for products in the "Beverages" category.
MATCH (p:Product)
WITH p, p.productName as productName, p.reorderLevel as reorderLevel
MATCH (p)-[:PART_OF]->(c:Category {categoryName: "Beverages"})
RETURN c.categoryName, productName, reorderLevelYou should profile the query and identify improvement opportunities. Consider the following questions:
-
What is the anchor node(s) in this query?
-
How can the query be simplified to reduce the number of operations?
-
What properties are being searched?
Click to reveal the solution
-
The query is overly complicated and is performing unnecessary operations, such as using
WITHto read properties that are not needed until later in the query. -
The anchor for the query is all the
Productnodes.
You can simplify the query and make the
Categorynodes the anchor:cypherSimplify the query to make Category the anchorMATCH (p:Product)-[:PART_OF]->(c:Category {categoryName: "Beverages"}) RETURN c.categoryName, p.productName as productName, p.reorderLevel as reorderLevel
-
The query is searching for
categoryNameon theCategorynodes, you can also create an index on that property:cypherCreate an index on categoryNameCREATE INDEX categoryName_Category IF NOT EXISTS FOR (c:Category) ON c.categoryName
The simplified query with the new index significantly improves the performance.
Lesson Summary
In this challenge, you investigated query performance issues, using various diagnostic approaches, and implementing solutions to resolve common performance problems.