Skip to content Skip to sidebar Skip to footer

App Engine Datastore In Operator - How To Use?

Reading: http://code.google.com/appengine/docs/python/datastore/gqlreference.html I want to use: := IN but am unsure how to make it work. Let's assume the following class User(

Solution 1:

Since you have a list of keys, you don't need to do a second query - you can do a batch fetch, instead. Try this:

#and this should get me the items that a user saveduseritems = db.get(saveditemkeys)

(Note you don't even need the guard clause - a db.get on 0 entities is short-circuited appropritely.)

What's the difference, you may ask? Well, a db.get takes about 20-40ms. A query, on the other hand (GQL or not) takes about 160-200ms. But wait, it gets worse! The IN operator is implemented in Python, and translates to multiple queries, which are executed serially. So if you do a query with an IN filter for 10 keys, you're doing 10 separate 160ms-ish query operations, for a total of about 1.6 seconds latency. A single db.get, in contrast, will have the same effect and take a total of about 30ms.

Solution 2:

+1 to Adam for getting me on the right track. Based on his pointer, and doing some searching at Code Search, I have the following solution.

usersaveditems = User.Gql(“Select*from UserListOfSavedItems whereuser=:1”, userkey)

saveditemkeys = []

for item in usersaveditems:
    #this should create a list of keys (references) to the saved item table
    saveditemkeys.append(item.str())    

if len(usersavedsearches >0):
    #and this should get me the items that a user saved
    useritems = db.Gql(“SELECT*FROM UniqueListOfSavedItems WHERE __key__ in :1’, saveditemkeys)

Post a Comment for "App Engine Datastore In Operator - How To Use?"