Gremlin:找到两个顶点之间的边的有效方法是什么? [英] Gremlin: What's an efficient way of finding an edge between two vertices?

查看:262
本文介绍了Gremlin:找到两个顶点之间的边的有效方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,很明显,在两个顶点之间找到边的简单方法是:

So obviously, a straight forward way to find an edge between two vertices is to:

graph.traversal().V(outVertex).bothE(edgeLabel).filter(__.otherV().is(inVertex))

我认为filter步骤将必须遍历所有边缘,这对于某些具有很多边缘的应用程序来说确实很慢.

I feel that filter step will have to iterate through all edges making really slow for some applications with a lot of edges.

另一种方式可能是:

traversal = graph.traversal()
                  .V(outVertex)
                  .bothE(edgeLabel)
                  .as("x")
                  .otherV()
                  .is(outVertex) // uses index?
                  .select("x");

我假设第二种方法可能更快,因为它将使用ID索引,这将使其比第一种方法更快.

I'm assuming the second approach could be much faster since it will be using ID index which will make it faster than the first the approach.

哪个(在IO方面)更快,更高效?

Which one is faster and more efficient (in terms of IO)?

我正在使用Titan,所以您也可以针对您的答案进行Titan设置.

I'm using Titan, so you could also make your answer Titan specific.

就时间而言,第一种方法似乎更快(顶点b的边缘为20k)

In terms of time, seems like the first approach is faster (edges were 20k for vertex b

gremlin> clock(100000){g.V(b).bothE().filter(otherV().is(a))}
==>0.0016451789999999999
gremlin> clock(100000){g.V(b).bothE().as("x").otherV().is(a).select("x")}
==>0.0018231140399999999

IO呢?

推荐答案

我希望第一个查询更快.但是,几件事了:

I would expect the first query to be faster. However, few things:

  1. 所有查询都不是最佳选择,因为它们都可以进行路径计算.如果您需要双向查找连接,请使用2个查询(我将在下面提供示例)
  2. 使用clock()时,请确保iterate()遍历,否则,您只会测量不执行任何操作所需的时间.
  1. None of the queries is optimal, since both of them enable path calculations. If you need to find a connection in both directions, then use 2 queries (I will give an example below)
  2. When you use clock(), be sure to iterate() your traversals, otherwise you'll only measure how long it takes to do nothing.

这些是我要在两个方向上找到边的查询:

These are the queries I would use to find an edge in both directions:

g.V(a).outE(edgeLabel).filter(inV().is(b))
g.V(b).outE(edgeLabel).filter(inV().is(a))

如果您希望获得最多的优势:

If you expect to get at most one edge:

edge = g.V(a).outE(edgeLabel).filter(inV().is(b)).tryNext().orElseGet {
       g.V(b).outE(edgeLabel).filter(inV().is(a)).tryNext()
}

通过这种方法,您无需进行路径计算.这些查询的执行方式在很大程度上取决于基础图形数据库. Titan的查询优化器可以识别该查询模式,并且几乎不会在任何时候返回结果.

This way you get rid of path calculations. How those queries perform will pretty much depend on the underlying graph database. Titan's query optimizer recognizes that query pattern and should return a result in almost no time.

现在,如果要测量运行时间,请执行以下操作:

Now if you want to measure the runtime, do this:

clock(100) {
  g.V(a).outE(edgeLabel).filter(inV().is(b)).iterate()
  g.V(b).outE(edgeLabel).filter(inV().is(a)).iterate()
}

这篇关于Gremlin:找到两个顶点之间的边的有效方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆