Neo4j - 索引和性能

Neo4j - Indexes and performance

我对 Neo4j 中 Cypher 查询的性能有疑问。 情况是:我图中的每个节点都有一个 属性,我想计算具有相同 属性 的节点数。 所以,我的基本查询是

match (n:NodeLabel)
with n.community as community, n.myid as myid
match (m) where m.community = community
return myid, count(m) as totcommunity

我在 属性 "community"

上创建了一个索引
create index on :NodeLabel(Community)

但性能很差:具有 200.000 个节点的图需要很长时间。 我怎样才能获得更好的性能?

提前致谢

如果您想return每个不同的社区值和具有该值的节点数:

MATCH (n:NodeLabel)
RETURN n.community, COUNT(n);

相反,如果您想获取与特定节点具有相同 community 值的节点数(如我的 myid 所标识),请尝试以下查询:

MATCH (n:NodeLabel {myid: 123})
WITH n.community AS c, n.myid AS myid
MATCH (n:NodeLabel)
WHERE n.community = c
RETURN myid, n.community, COUNT(n);

您忘记为第二个匹配项添加标签,因此无法使用索引: 试试这个:

match (n:NodeLabel)
match (m:NodeLabel) where m.community = n.community
return n.myid as myid, count(*) as totcommunity

您还可以通过在查询前加上 EXPLAIN 并检查查询计划来确保它使用索引。