Mahi Tej Gvp Mahi Tej Gvp - 2 months ago 13
Groovy Question

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

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

For example to sort a set of Vertices with label Employee based on their date of joining.

Which is a better way of retrieval?

Is it

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


or

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


here I've used
<=>
operator in groovy script and in the second one I've used plain order step in tinkerpop.

Both the queries work in gremlin console but which one of them is best for retrieval and how does the gremlin console interpret both the queries

Answer

It is better to avoid groovy collection methods, because an underlying graph database cannot interpret them. They exist outside of the Traversal. Consider this example that's like yours:

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]])]

When you do an explain() operation, you can see how the Traversal looks to the underlying graph database. If the database was capable of optimizing on the order() in this case, it could take advantage of that and become more efficient. If you simply submitted, g.V().hasLabel("person") and then switched to groovy collection methods, the underlying database would only know that you were trying to get the list of "person" vertices and not know you also intended to order them (which would then happen in memory with the use of the groovy sort).

Comments