Neo4J在聚合查询上的表现

时间:2012-11-14 15:37:44

标签: neo4j cypher

鉴于以下架构:(抱歉,它是弯曲的)

Schema

我正在尝试取回一个地方的活动清单,以及该地方特定活动的数量。

start root = node(0)
> match root-[:HAS_PLACE]->place-[:CAN_PLAY]->activity<-[:CAN_PLAY]-place2<-[?:PLAYS_AT]-user
> where place.Id = 1307
> return activity, count(user)
==> +---------------------------------------------------+
==> | activity                            | count(user) |
==> +---------------------------------------------------+
==> | Node[5]{Name->"Swimming",Id->5}     | 0           |
==> | Node[3]{Name->"Kick Boxing",Id->3}  | 0           |
==> | Node[12]{Name->"Basketball",Id->22} | 0           |
==> | Node[13]{Name->"Handball",Id->23}   | 0           |
==> | Node[6]{Name->"Racquetball",Id->6}  | 0           |
==> | Node[2]{Name->"Aerobics",Id->2}     | 0           |
==> +---------------------------------------------------+
==> 6 rows, 21403 ms 

虽然我认为上面做了我想要的,但是花21秒并不是一件好事。我相信这是由于可选关系甚至回溯,但我怎样才能提出更好的查询?

1 个答案:

答案 0 :(得分:4)

尝试:

start root = node(0)
match root-[:HAS_PLACE]->place-[:CAN_PLAY]->activity, user-[?:PLAYS_AT]->place
where place.Id = 1307
return activity, count(user)

或在placenodes上使用索引并执行:

start place = node:places(Id=1307)
match place-[:CAN_PLAY]->activity, user-[?:PLAYS_AT]->place
return activity, count(user)