首页 > 解决方案 > 在 Gremlin + Neptune 中,Inject() 要作为顶点插入的字符串列表需要很长时间

问题描述

在优化以 AWS Neptune 作为后端的服务时,我偶然发现了使用的建议inject(),因此我决定批处理一组查询以避免往返 Neptune:

g.inject(Array.from(attributes))
        .unfold()
        .as(SELECTOR_NAME)
        .coalesce(
            __.V()
                .hasLabel(NodeType.Attribute)
                .has(NodeType.propName.tenantId, this.tenantId)
                .has(NodeType.propName.code, __.where(Predicate.eq(SELECTOR_NAME))),
            __.addV(NodeType.Attribute)
                .property(NodeType.propName.created, new Date())
                .property(NodeType.propName.tenantId, this.tenantId)
                .property(NodeType.propName.updated, new Date())
                .property(NodeType.propName.code, __.identity())
                .property(NodeType.propName.title, __.identity()) // TODO capitalize or get from source
        )
        .project(NodeType.propName.code, NodeType.propName.id)
            .by(__.values(NodeType.propName.code))
            .by(__.id())
        .toList();

但是,对于 27 个项目,这需要 90 到 100 秒,这比单个upserts慢。我知道这里可能有很多问题,但我想排除查询本身的低效率。

使用的堆栈是 Typescript + Node.js + gremlin + AWS Neptune,但也通过 Sagemaker 笔记本查询大约需要 90 秒。

下面是一个应该在任何地方都可以使用的示例查询:

    g.inject(["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", '24', '25']).
    unfold().
    as('a')
.coalesce(
  __.V()
  .hasLabel('attribute')
  .has('tenantId', 'spm')
  .has('code', __.where(eq('a'))),
  __.addV('attribute').
  property('created', 'new Date()').
  property('tenantId', 'spm').
  property('updated', 'new Date()').
  property('code', __.identity()).
  property('title', __.identity())
)
.project("code", "id").by(__.values("code")).by(__.id())
.toList()

标签: typescriptgremlintinkerpoptinkerpop3amazon-neptune

解决方案


响应时间为72 毫秒,经过大量试验和错误,但主要解决方案是使用withSideEffect(),而不是inject()

g.withSideEffect("aList", ["a", "b", "c", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "x", 'y', 'z'])
.V()
.hasLabel('test_attribute')
.has('tenantId', 'spm')
.fold()
.as('v')
.select('aList')
.unfold()
.as('a')
.map(
__.coalesce(
      __.select('v').unfold().has("code", where(eq('a'))),
      __.addV('test_attribute').
      property('created', 'new Date()').
      property('tenantId', 'spm').
      property('updated', 'new Date()').
      property('code', __.identity()).
      property('title', __.identity())
)
)
.project("code", "id").by(__.values("code")).by(__.id())
.toList()

推荐阅读