views:

50

answers:

2

In a python based facebook application on GAE, i want to check which friends of current user have "marked" a web page or not.

For this i have to run as many DB queries as the number of friends (say 100) I fear this may run into "timeout" because of large no of queries.

Google DOCs suggest that "list" queries run in parallel, will this save time ?? Also list has a limit of 30, so i have to make 2 or 3 queries of list type.

Please suggest a better way if possible, using task ques or something....

+1  A: 

I would suggest the following:

  • Make 'marked' entities child entities of the users who have marked them.
  • Use a key name for the 'marked' entity that is based on the URL of the page marked
  • To find friends who have marked a page, retrieve a list of friends, then generate the list of entity keys from the list of friends (easy, since you know the friend key and the URL), and do a single batch get to retrieve a list of 'mark' entities indicating which friends have marked that page.
Nick Johnson
+1  A: 

You can fetch up to 1000 entities in parallel if you already know their keys or their key names.

There are a few ways to solve your specific problem. Here are is one.

Let's assume that when a user "marks" a web page, you create an entity with a key_name that derives from a user's facebook id and the page key.

class PageMarker(db.Model):
    user = db.ReferenceProperty(AppUser)
    ....
    @classmethod
    def mark_page(cls, user, page_key):
        marker = cls.get_or_insert("%s_%s" % (user.facebook_id, 
                                             page_key, user=user)

This allows you to fetch all the users who marked a page in parallel:

key_names = ["%s_%s" % (friend.facebook_id, page_key) for friend in friends]
markers = db.get(key_names)
# Use get_value_for_datastore to get the entity key without making a trip to the 
# datastore
friends_who_bookmarked_keys = [marker.__class__.user.get_value_for_datastore(marker)\
                          for marker in markers]
friends = db.get(friends_who_bookmarked_keys)
mahmoud
A Lot Thanks for ur response, really helpful, but I am sorry for not asking it correct and complete, but I want query result sorted by user rating ( 1 - 5), should I sort the results in Python Code or try to get it sorted SOME HOW from Query, which is less costly
Tiwari