2014-10-09 89 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) 
相关问题