简体   繁体   English

优化 gremlin 查询以避免图的多次遍历

[英]Optimizing gremlin query to avoid multiple traversals of graph

I am little new to Gremlin query paradigm.我对 Gremlin 查询范式并不陌生。 I have following gremlin query to get all the nodes related to node of type foo .我有以下 gremlin 查询来获取与foo类型的节点相关的所有节点。

g.V().hasLabel('foo').as('foo')
.coalesce(out('hasBar'), constant('')).as('bar')
.select('foo').coalesce(out('hasDelta'), constant('')).as('Delta')
.select('foo').coalesce(out('hasBar').out('hasGamma'), constant('')).as('Gamma')
.select('foo', 'bar', 'Delta', 'Gamma')

However this is not the optimized one as I have to traverse the graph multiple times and slows down the query execution.但是,这不是优化的,因为我必须多次遍历图表并减慢查询执行速度。

Edit编辑

Sample Data -样本数据 -

g.addV('foo').property('id', '1').property('p1', '1234').property('pk', 1)
g.addV('bar').property('id', '2').property('p2', '12345').property('pk', 1)
g.addV('Gamma').property('id', '3').property('p3', '123').property('pk', 1)
g.addV('Delta').property('id', '4').property('p4', '12').property('pk', 1)
g.V('1').addE("hasBar").to(g.V('2'))
g.V('1').addE("hasGamma").to(g.V('3'))
g.V('2').addE("hasDelta").to(g.V('4'))
g.addV('foo').property('id', '5').property('p1', '12345').property('pk', 1)
g.V('5').addE("hasBar").to(g.V('2'))
g.V('5').addE("hasGamma").to(g.V('3'))
g.addV('foo').property('id', '6').property('p1', '1').property('pk', 1)
g.V('6').addE("hasBar").to(g.V('2'))
g.V('6').addE("hasGamma").to(g.V('3'))
g.addV('foo').property('id', '7').property('p1', '145').property('pk', 1)
g.V('7').addE("hasBar").to(g.V('2'))
g.V('7').addE("hasGamma").to(g.V('3'))
g.addV('foo').property('id', '8').property('p1', '15').property('pk', 1)
g.addV('bar').property('id', '9').property('p2', '78').property('pk', 1)
g.addV('Gamma').property('id', '10').property('p3', '1236').property('pk', 1)
g.addV('Delta').property('id', '11').property('p4', '1258').property('pk', 1)
g.V('8').addE("hasBar").to(g.V('9'))
g.V('8').addE("hasGamma").to(g.V('10'))
g.V('10').addE("hasDelta").to(g.V('11'))

Previously I was fetching all foo and then was querying the corresponding bar, gamma and delta, which is very inefficient, so changed the query to fetch all at once, but now I am doing the same thing, but avoiding network calls.以前我是获取所有的 foo 然后查询对应的 bar、gamma 和 delta,效率非常低,所以将查询更改为一次全部获取,但现在我在做同样的事情,但避免了网络调用。

Above query gives following response -上述查询给出以下响应 -

[
{
    foo: {},
    bar: {},
    Delta: {},
    Gamma: {}
},
{
    foo: {},
    bar: {},
    Delta: {},
    Gamma: {}
}
]

You could just take advantage of labels and use the path step:您可以利用标签并使用path步骤:

g.V().hasLabel('foo').
      outE('hasBar','hasDelta','hasGamma').
      inV().
      path().by(label)

If you want to identify the vertices by a property or their ID adding a second by modulator after the path step will do that.如果您想通过属性或它们的 ID 来识别顶点,则在path步骤之后by调制器添加第二个即可。

g.V().hasLabel('foo').
      outE('hasBar','hasDelta','hasGamma').
      inV().
      path().
        by(id).
        by(label)

The paths returned will be of the form (I just assumed numeric IDs):返回的路径将采用以下形式(我只是假设数字 ID):

[1,hasBar,10]
[1,hasDelta,15]
[1,hasGamma,27]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM