【问题标题】:Gremlin order() step not natively supported?Gremlin order() 步骤本身不支持?
【发布时间】:2021-07-07 03:31:48
【问题描述】:

在分析缓慢的 gremlin 遍历时遇到警告。

警告:>> OrderGlobalStep([[[CoalesceStep([[VertexStep(IN,[view],edge), ProfileStep, NeptuneHasStep([isActive.eq(true)]), ProfileStep, EdgeVertexStep(OUT), ProfileStep, NeptuneHasStep([~id.eq(63b944e2-481d-42c8-a1a3-c0bc3ad24484)]), ProfileStep, RangeGlobalStep(0,1), ProfileStep, CountGlobalStep, ProfileStep], [ConstantStep(0), ProfileStep]]), ProfileStep] , asc], [value(rekognitionModerationDate), desc], [value(createdDate), desc]])

分析器报告此步骤占用了总执行时间的 62%,因此我想对其进行优化。下面是完整遍历的简化版:

g.V()
.hasLabel("post")
.order()
.by(
  __.coalesce(
    __.inE("view")
    .has("isActive", true)
    .outV()
    .hasId(userId)
    .limit(1)
    .count(),
    __.constant(0)
  ),
  order.asc
)

目标是首先输出没有传入view 边的post 顶点。换句话说,显示请求用户尚未查看的帖子,然后是他们查看过的帖子。当前遍历有效,但速度很慢。如何将其重构为“原生”,以便更快地执行?

编辑:显然,问题在于 Neptune 没有对 order().by() 的原生支持,并带有自定义比较器,如下所述: https://docs.aws.amazon.com/neptune/latest/userguide/gremlin-step-support.html

我仍然对如何重构它以获得纯原生支持的想法感兴趣。

【问题讨论】:

    标签: gremlin amazon-neptune


    【解决方案1】:

    当前的 Amazon Neptune 查询引擎一般会优化 order ... by 步骤。但是,如果与order 关联的任何子遍历无法优化,则将导致整个步骤无法优化。正如您在文档中注意到的那样,在优化方面,当与 order 一起使用时,如今 by 调制器中的内容存在限制。同样值得注意的是合并步骤不会得到优化的情况。查询优化器非常擅长优化coalesce 步骤,但有一种情况今天它没有。这种情况是当 coalesce 的 LHS 和 RHS 产生不同类型的值或使用 constant 时。因此,例如,如果 coalesce 总是从每个可能的路径中产生一个可能会得到优化的顶点。但是,当 RHS 是 constant 时,通常会导致 coalesce 无法优化。

    您可以通过查询来观察这一点,例如

    g.V('3').coalesce(out().count(),constant(0))
    

    因为 CountGlobalStep 的结果与 ConstantStep 的结果类型不同。这并不总是意味着您会看到性能不佳,但这就是在这种情况下您会在配置文件中看到警告的原因。通常,当constantcoalesce 一起使用时,您将看到当前引擎版本的警告。与许多事情一样,这些都是时间点行为。

    但是,在您的具体情况下,我认为我们可以潜在地简化事情并优化查询。当您使用count 时,如果不存在路径,则计数将为0,而无需讨厌的coalesce。这是一个经过优化的航线示例。

    g.V().hasLabel('airport').
      order().
        by(in('route').count()).
      limit(10).
      project('code','count').
        by('code').
        by(in('route').count())
    

    产生

    1   {'count': 0, 'code': 'BVS'}
    2   {'count': 0, 'code': 'TWB'}
    3   {'count': 0, 'code': 'EKA'}
    4   {'count': 0, 'code': 'TKQ'}
    5   {'count': 0, 'code': 'ISL'}
    6   {'count': 0, 'code': 'RIG'}
    7   {'count': 0, 'code': 'INT'}
    8   {'count': 0, 'code': 'APA'}
    9   {'count': 0, 'code': 'BWU'}
    10  {'count': 0, 'code': 'BID'}
    

    【讨论】:

    • .fold().coalesce(__.unfold().values("createdDate"), __.constant("")) 的情况呢? values 步骤和 constant 步骤会返回相同的类型吗?您对优化该案例有什么建议吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-06
    • 1970-01-01
    相关资源
    最近更新 更多