Introduction
In this lesson, you will learn how to refactor your graph by adding indirect relationships between nodes
Adding indirect relationships between nodes can help improve traversal performance and create more efficient query patterns.
graph TD
Node1 -->|RELATIONSHIP| Node2
Node2 -->|RELATIONSHIP| Node3
Node1 -->|<i>INDIRECT_RELATIONSHIP</i>| Node3
Node1(("<b>Node</b>"))
Node2(("<b>Node</b>"))
Node3(("<b>Node</b>"))
%% Style the new relationship
linkStyle 2 stroke-dasharray: 5 5Employees in regions
Employees are associated with regions through their territories.
To find all employees in a region, you need to traverse from the Employee to the Territory and then to the Region.
MATCH (employee:Employee)-[:IN_TERRITORY]->(:Territory)-[:IN_REGION]->(region:Region)
RETURN DISTINCT employee.firstName, region.regionDescriptionAn indirect relationship
To optimize this query, you can create an indirect relationship between the Employee and the Region nodes.
graph TD
Territory -->|IN_REGION| Region
Employee -->|IN_TERRITORY| Territory
Employee -->|<i>PART_OF_REGION</i>| Region
Territory(("<b>Territory</b>"))
Region(("<b>Region</b>"))
Employee(("<b>Employee</b>"))
%% Style the new relationship
linkStyle 2 stroke-dasharray: 5 5Create the indirect relationship
The following Cypher query creates a PART_OF_REGION relationship between Employee and Region nodes based on the existing relationships:
MATCH (employee:Employee)-[:IN_TERRITORY]->(:Territory)-[:IN_REGION]->(region:Region) // (1)
MERGE (employee)-[p:PART_OF_REGION]->(region) // (2)
RETURN employee, p, region // (3)-
Find the
Employee, andRegionnodes through the existingIN_TERRITORYandIN_REGIONrelationships. -
Create a new
PART_OF_REGIONrelationship between theEmployeeandRegionnodes. -
Return the employee, the new relationship, and the region to verify
Query the new relationship
You can now query for employees in regions using the new PART_OF_REGION relationship:
MATCH (employee:Employee)-[:PART_OF_REGION]->(region:Region)
RETURN employee.firstName, region.regionDescriptionProfile the original query and the refactored query to compare their performance.
Deleting relationships
You can delete relationships using the DELETE clause. For example, to delete all PART_OF_REGION relationships:
MATCH (employee)-[p:PART_OF_REGION]->(region)
DELETE pEmployees and customers
Orders are connected to employees through the SOLD relationship and to customers through the PURCHASED relationship.
You challenge is to create an indirect SOLD_TO relationship between the Employee that sold an Order and the Customer who purchased the Order.
graph TD
Customer -->|PURCHASED| Order
Employee -->|SOLD| Order
Employee -->|<i>SOLD_TO</i>| Customer
Order(("<b>Order</b>"))
Customer(("<b>Customer</b>"))
Employee(("<b>Employee</b>"))
%% Style the new relationship
linkStyle 2 stroke-dasharray: 5 5Your challenge
To create the (Employee)-[:SOLD_TO]→(Customer) relationship you will need to:
-
Identify the employees and customers connected through orders.
-
Create a new
SOLD_TOrelationship between the employee and customer nodes. -
Query the new relationship to verify it was created correctly.
Profile the original query and the refactored query to compare their performance.
Click to reveal the solution
-
Identify the employees and customers connected through orders
cypherEmployees and customer relationshipsMATCH (employee:Employee)-[:SOLD]->(:Order)<-[:PURCHASED]-(customer:Customer) RETURN DISTINCT employee.firstName, customer.companyName -
Create a new
SOLD_TOrelationship between the employee and customer nodescypherCreate indirect <code>SOLD_TO</code> relationshipsMATCH (employee:Employee)-[:SOLD]->(:Order)<-[:PURCHASED]-(customer:Customer) MERGE (employee)-[s:SOLD_TO]->(customer) RETURN employee, s, customer -
Query the new relationship to verify it was created correctly
cypherEmployees and the customers they sold toMATCH (employee:Employee)-[s:SOLD_TO]->(customer:Customer) RETURN employee.firstName, customer.companyName
Lesson Summary
In this lesson, you learned about creating indirect relationships between nodes to optimize graph traversals and improve query performance.
In the next lesson, you will learn about normalizing properties to nodes.