Introduction
In this lesson, you will learn how to refactor your graph by adding specific node labels to improve query performance and semantic clarity.
Labels allow you to categorize nodes and relationships in your graph, making it easier to query and understand the structure of your data.
By adding specific labels to nodes, you can optimize query performance by reducing the search space and improving index utilization.
Managers
Currently, all employees are represented with the Employee label.
A manager can only be identified by the existence of a REPORTS_TO relationship.
graph TD
Employee -->|REPORTS_TO| Manager
Employee(("<b>Employee</b>"))
Manager(("<b>Employee</b><br /><b><i>Manager</i></b>"))Finding Managers
This query traverses all the Employee nodes and the REPORTS_TO relationships to find the managers.
MATCH (e:Employee)-[:REPORTS_TO]->(manager:Employee)
RETURN manager.firstNameBy adding a Manager label to the manager nodes, you can optimize this query.
Adding a Manager label
Set a Manager label to the Employee nodes that have an incoming REPORTS_TO relationship:
MATCH (e:Employee)-[:REPORTS_TO]->(manager:Employee)
SET manager:Manager
RETURN managerQuery the Manager label
You can now find all managers by querying for nodes with the Manager label:
MATCH (manager:Manager)
RETURN manager.firstNameCompare the PROFILE of the original query and the refactored query to see the performance difference.
Removing labels
You can remove a label from a node using the REMOVE clause. For example, to remove the Manager label from all nodes:
MATCH (e:Employee)-[:REPORTS_TO]->(manager:Employee)
REMOVE manager:Manager
RETURN managerAdding a ProductDiscontinued label
Your challenge is to create an ProductActive label.
Not all products are still available for sale. The discontinued property on the Product nodes indicates whether a product has been discontinued or not.
MATCH (p:Product)
WHERE p.discontinued = true
RETURN p.productNameTo complete the challenge you will need to:
-
Create a Cypher query to identify the active products.
-
Update the query to
SETa newProductActivelabel on the active products. -
Write a new query to find all active products using the new
ProductActivelabel.
Profile the queries before and after to see the performance difference.
Click to reveal the solution
-
You can identify the active products by checking for
discontinued = FALSE:cypherFind active productsMATCH (p:Product) WHERE p.discontinued = false RETURN p.productName -
You can set the
ProductActivelabel on the active products:cypherSet the labelMATCH (p:Product) WHERE p.discontinued = false SET p:ProductActive RETURN p.productName -
Query for active products using the new label:
cypherFind <code>ProductActive</code> nodesMATCH (p:ProductActive) RETURN p.productName
Finding discontinued products
To find all discontinued products, you can query for Product nodes and do not have the ProductActive label:
MATCH (p:Product&!ProductActive)
RETURN p.productNameLesson Summary
In this lesson, you learned about adding specific node labels to improve query performance and make your graph model more semantically clear.
In the next lesson, you will learn about adding indirect relationships between nodes.