Skip to content Skip to sidebar Skip to footer

Optimizing A Inequality Query In Ndb Over Two Properties

I'm trying to do a query into a range of valid dates q = Licence.query(Licence.valid_from <= today, Licence.valid_to >= today, ancestor =

Solution 1:

One solution would be to create a new field, like start_week, which buckets the queries and allows you to use an IN query to filter:

q = Licence.query(Licence.start_week inrange(5,30),
                  Licence.valid_to >= today,
                  ancestor = customer.key)

Even simpler: Use a projection query to identify the right set of data without fetching full entities. This is faster than a regular query.

it = Licence.query(License.valid_from <= today,
                   ancestor = customer.key
                   ).iter(projection=[License.valid_to])
keys = [e.key for e in it if e.valid_to >= today]
licenses = ndb.get_multi(keys)

Post a Comment for "Optimizing A Inequality Query In Ndb Over Two Properties"