graph - 如何提高计数查询的性能
问题描述
查询的目的是找到返回的节点和边的计数。查询如下:
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 它只返回边缘计数。
我们如何优化查询以返回顶点和边数,并在需要时限制顶点或边?
解决方案
我不确定我有什么突破性的东西可以提供,但看起来你的第二个查询可以通过删除不需要的处理来变得更快:
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
推荐阅读
- mysql - 致命错误:未捕获的错误:在 null 上调用成员函数存在()
- windows - 为多台机器使用 InstallShield 许可证
- swift - 存储在 Firestore 中的系统 Date 对象的行为将发生变化,您的应用程序可能会中断?
- c# - 如何在 VSTS 上配置单元测试名称/描述?
- log4j2 - Log4j2,如何处理访问被拒绝异常
- active-directory - 搜索过滤器中具有通配符模式的嵌套组的 Active Directory 搜索查询
- android - 无法获取 ttc 文件适用于 android 中的字体
- django - 按 id 批量删除 Django
- javascript - 用猫鼬更新值
- reactjs - 当我打开一个模式时,道具是空的 - React