2014-10-09 82 views
1

我有一個圖,其中實體Customer,Product和關係ORDERED。以下是使用cypher計算單個查詢中兩個節點的總數和它們之間的關係數 - neo4j

(Customer)-[:ORDERED]->(Product) 

我想計算產品的總數量,在一個單一的暗號查詢訂單客戶和總人數的總數暗號方式它們之間的關係。

下面是我寫的

單查詢

MATCH 
    (c:Customer)-[r:ORDERED]->(p:Product), 
    (p1:Product), 
    (c1:Customer) 
WITH  
    count(r) as order_count , 
    count(DISTINCT c1) as customer_count , 
    count(DISTINCT p1) as product_count 
RETURN order_count , customer_count , product_count 

查詢,但它給錯誤的結果與所有算作是相同的值執行了很長一段時間。

如果我執行各自獨立計數,然後給它的結果非常快速和正確

單獨的查詢

MATCH (c:Customer)-[r:ORDERED]->(p:Product) 
WITH count(r) as order_count 
RETURN order_count 

MATCH (p1:Product) 
WITH count(DISTINCT p1) as product_count 
RETURN product_count 

MATCH (c1:Customer) 
WITH count(DISTINCT c1) as customer_count 
RETURN customer_count 

誰能解釋什麼是在單個查詢回事?

回答

0

您的查詢是爆炸性的,生成三個初始匹配的叉積。爲了避免交叉產品,你可以:

MATCH (c:Customer) 
WITH COUNT(c) AS customer_count 
MATCH (p:Product) 
with cs, COUNT(p) AS product_count 
MATCH()-[r:ORDERED]->() 
RETURN customer_count, product_count, COUNT(r) AS order_count 

我不認爲你可以得到所有的計數沒有ORDERED關係的匹配。如果在其他類型的節點之間發生有序關係ip,您必須添加第二行到最後一行的交換:

MATCH (:Customer)-[r:ORDERED]->(:Product) 
相關問題