使用cypher-neo4j计算单个查询中两个节点的总数以及它们之间的关系数

使用cypher-neo4j计算单个查询中两个节点的总数以及它们之间的关系数,neo4j,cypher,graph-databases,Neo4j,Cypher,Graph Databases,我有一个包含实体客户、产品和关系订单的图表。以下是他们之间的关系,以密码的方式 (Customer)-[:ORDERED]->(Product) 我想在一个密码查询中计算产品总数、客户总数和订单总数 下面是我写的问题 单一查询 MATCH (c:Customer)-[r:ORDERED]->(p:Product), (p1:Product), (c1:Customer) WITH count(r) as order_count ,

我有一个包含实体
客户
产品
和关系
订单
的图表。以下是他们之间的关系,以密码的方式

(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  

有人能解释一下单个查询中发生了什么吗?

您的查询正在爆炸,生成三个初始匹配的叉积。要避免交叉积,您可以:

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
我不认为你可以得到所有的计数而不重新匹配有序的关系。如果有序关系SIP可以在其他类型的节点之间发生,则必须添加从第二行到最后一行的交换:

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