为什么这个Cypher查询需要花费太多时间?

Meh*_*taş 3 neo4j cypher

我有

  • 50K Post节点
  • 40K标记节点
  • 125K TAGGED关系(意味着每个帖子平均2,5个标签)

在我的图形和下面的查询中导致"Java堆空间"错误.

match (p1:Post)-[r1:TAGGED]->(t:Tag)<-[r2:TAGGED]-(p2:Post)
return p1.Title, count(r1), p2.Title, count(r2)
limit 10
Run Code Online (Sandbox Code Playgroud)

根据共享标签的数量,我期望的是一些重复的行.我不确定如何limit工作(在前10个帖子或标签后停止).但是,因为我limit 10没有想到这个查询遍历所有图形.看起来确实如此.

更新1

通过一些更改,Christophe Willemsen的查询在15秒内返回10行.

// I need label for the otherPost because Users are also TAGGED
MATCH (post:Post)-[:TAGGED]->(t)<-[:TAGGED]-(otherPost:Post) 
RETURN post.Title, count(t) as cnt, otherPost.Title
// ORDER BY cnt DESC // for now I do not need this
LIMIT 10;
Run Code Online (Sandbox Code Playgroud)

我认为"ORDER BY"子句可能会导致遍历所有可能的路径,因此我删除了该子句但它仍然是15秒.这也是15秒.当我在没有排序的情况下使限值1或1000.

我对Neo4j的期望是:"从任何Post节点开始,然后跳转到其标签,找到标记有相同标签的其他邮件.当有10个发现停止遍历并返回结果时." 我很确定它没有这样做.

为了明确我的期望,假设图表很小,我们Limit 3在cypher查询中使用.

p1 - [t1, t2, t3] // Post1 is tagged with t1, t2 and t3
p2 - [t2, t3, t4]
p3 - [t3, t4, t5]
Run Code Online (Sandbox Code Playgroud)

我的期望是:

  • 从表单p1(或任何Post节点)开始
  • 跳到t1
  • 没有其他帖子标记为t1
  • 跳到t2
  • p2标记为t2(1/3)
  • 没有其他帖子标有t2
  • 跳到t3
  • p2标记为t3(2/3)
  • p3标记为t3(3/3)
  • 我们达到极限,休息

但是,在遍历所有数据后似乎应用了Limit.

那么,我现在的问题是:Neo4j是否找到了所有的比赛并且返回了其中的10个或者在前10场比赛后停止了搜索?当然,为什么?

更新2

在有用的答案后,我设法减少了我的问题的范围,所以我尝试了以下查询.

// 3 sec.
MATCH (p:Post)-[:TAGGED]->(t:Tag)
RETURN p.Title, count(t)
LIMIT 1; 

// 3 sec.
MATCH (p:Post)-[:TAGGED]->(t:Tag)
RETURN p.Title, count(t)
LIMIT 1000; 

// 100 ms.
MATCH (p:Post)-[:TAGGED]->(t:Tag)
RETURN p.Title, t.Name
LIMIT 1; 

// 150 ms.
MATCH (p:Post)-[:TAGGED]->(t:Tag)
RETURN p.Title, t.Name
LIMIT 1000;
Run Code Online (Sandbox Code Playgroud)

所以,我仍然不知道为什么,但是,使用聚合方法(我试过collect(t.Name)而不是count)打破了预期的(至少我的期望:)行为的limit功能.

Chr*_*sen 6

此查询将导致全局图表查找,至少对于neo4j 2.1.7及更低版本.

我首先匹配节点,然后扩展路径

MATCH (post:Post)
MATCH (post)-[:TAGS]->(t)<-[:TAGS]-(otherPost)
RETURN post, count(t) as cnt, otherPost
ORDER BY cnt DESC
LIMIT 10;
Run Code Online (Sandbox Code Playgroud)

这是执行计划,正如您所看到的那样,首先只匹配帖子节点(因此标签索引),只需要检索这些和以下关系

ColumnFilter
  |
  +Top
    |
    +EagerAggregation
      |
      +Filter
        |
        +SimplePatternMatcher
          |
          +NodeByLabel

+----------------------+--------+--------+----------------------------------------------+------------------------------------------------------------------------------------------------+
|             Operator |   Rows | DbHits |                                  Identifiers |                                                                                          Other |
+----------------------+--------+--------+----------------------------------------------+------------------------------------------------------------------------------------------------+
|         ColumnFilter |     10 |      0 |                                              |                                                              keep columns post, cnt, otherPost |
|                  Top |     10 |      0 |                                              | {  AUTOINT0}; Cached(  INTERNAL_AGGREGATEc24f01bf-69cc-4bd9-9aed-be257028194b of type Integer) |
|     EagerAggregation |   9900 |      0 |                                              |                                                                                post, otherPost |
|               Filter | 134234 |      0 |                                              |                                                                NOT(  UNNAMED30 ==   UNNAMED43) |
| SimplePatternMatcher | 134234 |      0 | t,   UNNAMED43,   UNNAMED30, post, otherPost |                                                                                                |
|          NodeByLabel |    100 |    101 |                                   post, post |                                                                                          :Post |
+----------------------+--------+--------+----------------------------------------------+------------------------------------------------------------------------------------------------+

Total database accesses: 101
Run Code Online (Sandbox Code Playgroud)

这里有一篇博客文章,解释了除了查询的第一部分之外我删除标签的原因:http://graphaware.com/neo4j/2015/01/16/neo4j-graph-model-design-labels-versus-indexed-properties html的