开发者

App Engine reference property optimization

I have a database structure like this -

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

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

class开发者_Python百科 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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜