首页 > 解决方案 > 如何提高计数查询的性能

问题描述

查询的目的是找到返回的节点和边的计数。查询如下:

g.inject(1).union(V().has('property1', 'A').aggregate('v').outE().has('property1', 'E').aggregate('e').inV().has('property1', 'B').aggregate('v')).select('v').dedup().as('vertexCount').select('e').dedup().as('edgeCount').select('vertexCount','edgeCount').by(unfold().count())

输出:vertexCount:200k edgeCount:250k 耗时:1.5 分钟

我试图优化查询并尝试了以下操作:

g.inject(1).union(V().has('property1', 'A').as('v1').outE().has('property1', 'E').as('e').inV().has('property1', 'B').as('v2')).select('v1','e','v2').by(valueMap().by(unfold())).count()

输出:250k 耗时:30 sec 它只返回边缘计数。

我们如何优化查询以返回顶点和边数,并在需要时限制顶点或边?

标签: graphgremlinjanusgraph

解决方案


我不确定我有什么突破性的东西可以提供,但看起来你的第二个查询可以通过删除不需要的处理来变得更快:

g.V().has('property1', 'A').
  outE().has('property1', 'E').
  inV().has('property1', 'B').
  count()

我想如果“property1”(对于“A”)被编入索引,则删除inject()/union()将允许该索引受到打击(不确定 JanusGraph 是否会像inject()/一样优化该查询,union()而且似乎都没有用) . 根据“E”的“property1”的性质,以顶点为中心的索引也可能会有所帮助。这select().by()似乎是一种不必要且可能代价高昂的转换,因为它启用了路径跟踪并强制添加了一个Map您在count()

您的评论表明您需要源顶点和边的计数。也许这样的事情会起作用:

gremlin> g.V(1).aggregate('e').by(constant(1)).
......1>   outE().
......2>   inV().count().
......3>   math("(2 * _) + x").
......4>     by().
......5>     by(select('e').unfold().sum()) 
==>7.0

只是为您稍后在该步骤中aggregate()的列表中的每个源顶点保存一个“1” 。由于边的数量应该等于您的数量,因此只需将其乘以“2”,然后将该总和相加即可得到您要查找的数量。sum()math()inV()

或者如果边可以指向同一个目标顶点,只需将聚合模式扩展到边dedup()inV()

gremlin> g.V(1).aggregate('s').by(constant(1)).
......1>   outE().aggregate('e').by(constant(1)).
......2>   inV().dedup().count().
......3>   math("_ + source + edge").
......4>     by().
......5>     by(select('s').unfold().sum()).
......6>     by(select('e').unfold().sum())  
==>7.0

如果您不想计算与目标的完整路径不匹配的任何源顶点,也可以添加过滤:

gremlin> g.V(1).filter(outE().has('weight',gt(0)).inV().hasLabel('person','software')).
......1>   aggregate('s').by(constant(1)).
......2>   outE().has('weight',gt(0)).
......3>   aggregate('e').by(constant(1)).
......4>   inV().hasLabel('person','software').dedup().count().
......5>   math("_ + source + edge").
......6>     by().
......7>     by(select('s').unfold().sum()).
......8>     by(select('e').unfold().sum()) 
==>7.0

推荐阅读