Search code examples
azure-cosmosdbgremlinazure-cosmosdb-gremlinapi

Gremlin query unintentionally iterates whole graph


I'm trying to upsert two vertices in a single query. I'm doing the following query, but the second vertex somehow iterates the entire graph (see image clipped from executeprofile).

What am I misunderstanding here?

g
  .V('1')
  .fold()
  .coalesce(
    unfold(),
    addV('Company').property('id', '1').property('pk','1')
  )
  .aggregate('a')
  .V('2')
  .fold()
  .coalesce(
    unfold(), 
    addV('Person').property('id', '2').property('pk', '2')
  )
  .as('b')

Wholegraph iterated


Solution

  • Eventually, I managed to figure out the solution, albeit not the reason.

    I converted to (roughly) this:

    g
      .V('1')
      .fold()
      .coalesce(
        g.V('1'),
        addV('Company').property('id', '1').property('pk','1')
      )
      .aggregate('a')
      .coalesce(
        g.V('2'), 
        addV('Person').property('id', '2').property('pk', '2')
      )
      .as('b')
    

    So basically removing the second .V('2') and instead putting it inside the coalesce. I guess there must be some way to remove the very first V('1'), but I haven't quite figured that out yet.