编写 gremlin 查询的最佳方法是通过 groovy 脚本或 tinkerpop 步骤

What is the best way to write a gremlin query is it through groovy script or tinkerpop steps

编写 gremlin 查询的最佳方法是通过 groovy 脚本还是 tinkerpop 步骤?

例如,根据加入日期对带有标签 Employee 的一组顶点进行排序。

哪种检索方式更好?

是吗

g.V().hasLabel('Employee').sort{a,b->a.value('DateOfJoining')<=>b.value('DateOfJoining')}

g.V().hasLabel('Employee').order().by('DateOfJoining',incr)

这里我在 groovy 脚本中使用了 <=> 运算符,在第二个脚本中我在 tinkerpop 中使用了普通命令步骤。

这两个查询都可以在 gremlin 控制台中使用,但是哪一个最适合检索以及 gremlin 控制台如何解释这两个查询

最好避免groovy collection methods,因为底层图形数据库无法解释它们。它们存在于 Traversal 之外。考虑这个与您相似的示例:

gremlin> g.V().hasLabel("person").order().by('name',incr).explain()
==>Traversal Explanation
===============================================================================================================================
Original Traversal                 [GraphStep(vertex,[]), HasStep([~label.eq(person)]), OrderGlobalStep([[value(name), incr]])]

ConnectiveStrategy           [D]   [GraphStep(vertex,[]), HasStep([~label.eq(person)]), OrderGlobalStep([[value(name), incr]])]
RepeatUnrollStrategy         [O]   [GraphStep(vertex,[]), HasStep([~label.eq(person)]), OrderGlobalStep([[value(name), incr]])]
InlineFilterStrategy         [O]   [GraphStep(vertex,[]), HasStep([~label.eq(person)]), OrderGlobalStep([[value(name), incr]])]
MatchPredicateStrategy       [O]   [GraphStep(vertex,[]), HasStep([~label.eq(person)]), OrderGlobalStep([[value(name), incr]])]
PathRetractionStrategy       [O]   [GraphStep(vertex,[]), HasStep([~label.eq(person)]), OrderGlobalStep([[value(name), incr]])]
IncidentToAdjacentStrategy   [O]   [GraphStep(vertex,[]), HasStep([~label.eq(person)]), OrderGlobalStep([[value(name), incr]])]
AdjacentToIncidentStrategy   [O]   [GraphStep(vertex,[]), HasStep([~label.eq(person)]), OrderGlobalStep([[value(name), incr]])]
FilterRankingStrategy        [O]   [GraphStep(vertex,[]), HasStep([~label.eq(person)]), OrderGlobalStep([[value(name), incr]])]
RangeByIsCountStrategy       [O]   [GraphStep(vertex,[]), HasStep([~label.eq(person)]), OrderGlobalStep([[value(name), incr]])]
TinkerGraphStepStrategy      [P]   [TinkerGraphStep(vertex,[~label.eq(person)]), OrderGlobalStep([[value(name), incr]])]
ProfileStrategy              [F]   [TinkerGraphStep(vertex,[~label.eq(person)]), OrderGlobalStep([[value(name), incr]])]
StandardVerificationStrategy [V]   [TinkerGraphStep(vertex,[~label.eq(person)]), OrderGlobalStep([[value(name), incr]])]

Final Traversal                    [TinkerGraphStep(vertex,[~label.eq(person)]), OrderGlobalStep([[value(name), incr]])]

当您执行 explain() 操作时,您可以看到 Traversal 对底层图形数据库的看法。在这种情况下,如果数据库能够在 order() 上进行优化,它就可以利用这一点并变得更有效率。如果你简单地提交,g.V().hasLabel("person") 然后切换到 groovy 收集方法,底层数据库只会知道你正在尝试获取 "person" 顶点的列表而不知道你也打算对它们进行排序(然后使用 groovy sort 在内存中发生)。