Take the 2-minute tour ×

I'd like to delete a bunch of objects from the AppEngine datastore within a transaction:

def F():
  items_to_delete = []
  for item in db.Query().ancestor(...):
    if item.aaa ... item.bbb:

Is it possible to fetch only a few fields of the items (aaa and bbb)? Will such a fetch have a positive effect on performance?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

It's not possible to fetch only a few fields; i.e. you'll always be fetching the entire entity with all its fields (or just the entity keys). From the App Engine GQL reference docs:

The GQL syntax can be summarized as follows:   
    SELECT [* | __key__]
        [FROM <kind>]]
        [WHERE <condition> [AND <condition> ...]]
        [ORDER BY <property> [ASC | DESC] [, <property> [ASC | DESC] ...]]
        [LIMIT [<offset>,]<count>]
        [OFFSET <offset>]


A GQL query returns zero or more entities or Keys of the requested kind. Every GQL query always begins with either SELECT * or SELECT __key__. (A GQL query cannot perform a SQL-like "join" query.) Tip: SELECT __key__ queries are faster and cost less CPU than SELECT * queries.

share|improve this answer
So how do I fetch only the __key__ in an db.Query().ancestor(...) query? –  pts Nov 24 '11 at 23:57
@pts Try db.Query(keys_only=True).ancestor(...): code.google.com/appengine/docs/python/datastore/… –  David Hu Nov 25 '11 at 1:24

Check out NDB tutorial, written by Guido himself (bow to ground and say "I'm not worthy" x 3 :). He uses AND OR in query and fetching a few fields with offset (that answers your question). Still experimental though, so use with caution until it is out of preview.

share|improve this answer

Your Answer


