Database Reference
In-Depth Information
Now that we have this hierarchy in place, we can start doing some queries on it. One
of the key problems that we would like to solve is the price calculation problem: how
do we run through the entire hierarchy structure and use the information stored
inthehierarchyto(re)calculatethepriceofaproduct?Thiswouldtypicallybea
terriblydificultandtime-consumingquery(asamatteroffact,itisaive-wayjoin
operation) on a relational system. So, what would it be on a graph-based database
suchasNeo4j?Let'sindout.
In the following sections, we will actually present two different strategies to
calculate the price of the product using the product hierarchy.
Theirststrategywillbebyrunningthroughtheentiretree,takingthelowestpart
of the tree for the pricing information and then multiplying that with the quantity
information of all the relationships. When this full sweep of the tree reaches the top,
we will have calculated the entire price of the product.
The second strategy will be using a number of intermediate calculations at every
level of the tree. We will update all of the intermediate levels of the tree with the
price calculations of everything lying "underneath" it at lower levels of the tree so
that, effectively, we would need to run through a much smaller part of the tree to
(re)calculate the price.
Let's take a look at this in more detail.
Calculating the price based on a full sweep of
the tree
Theirst strategy we will use to calculate the price of the product in our hierarchy
will to a full sweep of the tree:
1.
It will start from the product at the top.
2.
Then, it will work its way down to every single cost component in
the database.
3.
Then, it will return the sum of the all total prices, calculated as a product
of the price of each cost component with the quantities on the relationships
that connect the cost component to the product at the top.
The query for this approach is similar to the following:
match
(n1:PRODUCT {id:1})<-[r1]-(:COST_GROUP)<-[r2]-(:COST_TYPE)<-[r3]-
(:COST_SUBTYPE)<-[r4]-(:COST)<-[r5]-(n6:COST_COMPONENT)
return sum(r1.quantity*r2.quantity*r3.quantity*r4.quantity*r5.
quantity*n6.price) as CalculatedPrice;
 
Search WWH ::




Custom Search