Gremlin: What's an efficient way of finding an edge between two vertices?

前端 未结 2 1958
傲寒
傲寒 2021-01-13 04:44

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

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


        
2条回答
  •  情深已故
    2021-01-13 04:55

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

    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()
    }
    

    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()
    }
    

提交回复
热议问题