0

I'm working on converting an application from db to ndb, and am working on making my queries more efficient. The application has become a bit slow with the increase in amount of data, causing us to realize that the queries are pretty inefficient.

Background:

Our stripped-down data model is similar to this (feel free to criticize):

class ServiceRequest(ndb.Model):
  technician = ndb.UserProperty()
  application = ndb.KeyProperty()  # ServiceRequestApplication kind


class ServiceRequestApplication(ndb.Model):
  applicant = ndb.UserProperty()

Admins can "approve" a ServiceRequestApplication for a ServiceRequest. ServiceRequestApplications are instantiated with a parent ServiceRequest. If a ServiceRequestApplication is approved, the application's key is stored in the request's application property.

The tricky part:

There can be multiple ServiceRequest entities for 1 actual service appointment (1 per technician). We have one ServiceRequest, and if there are multiple techs needed, we create additional ServiceRequests, with the main ServiceRequest as a parent. We originally approached the problem this way because ancestor queries would be pretty easy, including some other reasons with regards to the way this data was going to be queried/represented.

Can anyone think of a way to query for ServiceRequest entities that don't have children ServiceRequests?

Thanks!

1 Answers1

1

In the subject you ask about querying for entities with no parents; in the body you ask about querying for entities with no children. In either case, though, the answer is the same: This isn't possible with standard App Engine indexes. The former requires searching for something that is not present in the key (which isn't possible the way indexes are structured), and the latter requires a join, since it's selecting entities based on properties (or presence) of other entities.

Instead, you will need to denormalize: add a property to ServiceRequest that indicates if it's a root entity, or if it has children, as appropriate.

Nick Johnson
  • 100,655
  • 16
  • 128
  • 198