views:

43

answers:

1

I have a database structure like this -

class Movie(db.Model):
    name = db.StringProperty()

class Tag(db.Model):
    name = db.StringProperty()

class MovieTag(db.Model):
    movie = db.ReferenceProperty(Movie, collection_name='tags')
    tag = db.ReferenceProperty(Tag, collection_name='movies')

I have a query where I am trying to retrieve all the movies with the tags. I have a query like this.

query = Movie.all()
movies = [{"name":movie.name,  
           "tags":[t.tag.name for t in movie.tags]} for movie in query]

However, this is taking very long time specially with a large number of movies (about 400). How do I optimize this? I have tried memcaching but the first call is still very slow and causes the 30s response timeout.

+5  A: 

You should model your entities something like this:

class Movie(db.Model):
  name = db.StringProperty()
  tags = db.ListProperty(db.Key)

class Tag(db.Model):
  name = db.CategoryProperty()
  @property
  def movies(self):
    return Movie.gql("WHERE tags = :1", self.key())

In this scheme you can call Tag.movies to get all the movies that belong to a certain tag.

More info at GAE: Modeling Entity Relationships

Shay Erlichmen
thanks...I will give it a try :)
lamirap
Alternately, model a movie's tags as a StringListProperty, and use the tag name as the key_name in the tag model.
Nick Johnson