AppEngine docs note that “Queries that return keys are faster and cost less CPU than queries that return full entities.”
Which of these two is the more desired way to fetch and iterate over a number of objects from the data store?
query_keys = Person.all(keys_only=True)
query_keys.filter('name = ', person_name)
query_keys.order("__key__")
people = db.get(query_keys)
for p in people:
#read properties of the person object
print p.name
Vs
query = Person.all()
query.filter('name = ', person_name)
query.order("__key__")
for p in people:
#read properties of the person object
print p.name
A regular query performs essentially the operations that your first snippet does, only inside a single RPC. If you need the entities, it’s more efficient to just ask for them, than to ask for just the keys and fetch the entities yourself.
One thing you should watch out for efficiency-wise is iterating over Query objects. When you do this, the underlying RPC layer fetches results from the datastore in batches of 20, resulting in a lot of unnecessary RPCs. If you know how many results you need, you should call
fetch()on the query (likeresults = Person.all().filter('name =', 'Joe').fetch(100)), which will only execute a single RPC.Appstats is an excellent tool for discovering and diagnosing these sorts of performance issues.