Efficient increment of facet values

mutation

(SaS) #1

Hi,

I would like to know if there is a way to efficiently increment a facet value without having to go through the application layer.
I’m looking at a data model that involves observations and observation points, with a “seen_by” edge going into the observation point node from an observation, which facets on that edge for the observation count on this location, the first seen and the last seen date. The problem here is that I am aiming for a high number of updates, i.e. I have a constant stream of observations to update the graph with, most of which are already existing as nodes. So at the moment I would need to do a separate query per added observation to:

  • check whether the observation already exists and get its uid
  • check whether the edge to the indicated observation point already exists and get its uid
  • obtain the current count facet value if the edge exists

Then I can selectively construct a mutation that creates the non-existing nodes and updates/creates the edge, adding the new count to the existing count on the edge.
Setting first_seen on the edge can be done via an additional conditional mutation if it is not present. Last_seen will always be updated.

I can see that with conditional upsert blocks I might be able to handle the edge and node checking and creation, but one thing I do not see how to address is the counter increment. Is there an efficient way to do this on the database side? I would like to be able to handle thousands of updates per second, and any round-trip I can shave of the execution time would help.

Thanks!


(Michel Conrado) #2

Nope, see https://github.com/dgraph-io/dgraph/issues/1996 for more details.

For increment I have an example

upsert{
  query{
    var(func: eq(campaignId, 1)){
      ts as timestamp
      incr as math(ts + 1)
    }
  }


  mutation {
    set{
         uid(campaignVar) <dgraph.type> "Campaign" .
            uid(campaignVar) <campaignId> "1" .
            uid(campaignVar) <enabled> "false" .
            uid(campaignVar) <timestamp> val(incr) . 
    }
  }
}

This approach does not work with Facets, as variables in Facets are not supported for now.

Ref


(SaS) #3

Thanks! I’ll see if I can work around that by doing more aggregation to reduce the number of actual mutations.