typescript - 在 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()
解决方案
响应时间为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()
推荐阅读
- tfs - Team Foundation Server 集合中的总代码行数?
- bash - 如何用 shell 脚本的参数替换文件中的特定行?
- rust - 为什么 &str 从函数返回时没有“寿命不够长”的问题?
- azure - Azure 政府何时提供托管容器服务 (AKS)?
- vba - 替换句子中的单词
- html - 对于两种类型的列(固定宽度和流体宽度),对于 flex-nowrap (bootstrap 4),overflow-x:auto 不能按预期工作
- php - 从递归函数php获取数据
- r - 在 R 中导入原始图像
- lua - Lua 修复了 tile 移动问题
- ruby - 如何构造与多字符模式的第一个示例匹配的 Ruby 正则表达式?