Z
Z
zencd2012-03-28 22:37:28
Python
zencd, 2012-03-28 22:37:28

Reducing the number of reads in GAE. DS modeling question

Let's say there is a page displaying one article. The article has 4 tags. And there are two options for implementing this in the DataStore. The first is normal - the article refers to records in another table:

class Article(db.Model):
  tags = db.ListProperty(db.Key)


The second, less flexible option - the article does not contain external links, but contains the names (strings) of tags in itself.

class Article(db.Model):
  tags = db.StringListProperty()


An article is selected from the database simply: Article.get(key)

Question . Is it possible to say that a page request will cause 5 reads in the first case, and only one in the second? (Reads are those that count against quota control.)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
avalak, 2012-03-29
@avalak

How to profile an application:

import os

# ...

if os.getenv('SERVER_SOFTWARE', '').startswith('Dev'):
  # http://localhost:8080/_ah/stats/ - тут появится подробная статистика: запросы, время, вызовы api
  from google.appengine.ext.appstats import recording
  app = recording.appstats_wsgi_middleware(app)

In the first case, you can get by with two calls (0 when caching the selection in gae memcache):
class Artwork(db.Model):
  refs = db.ListProperty(db.Key)
  @property
  def taxonomy(self):
    return db.get(self.refs)

You can reduce the number of calls by passing multiple values ​​to db.get and db.put at once
# 100 вызовов
for tag in tags: db.put(Tag(tag))

# 1 вызов
db.put([Tag(tag) for tag in tags])

In the second case, there will be only one RPC call, because strings are stored (0 when caching).
Well, if possible, you need to cache the selection in gae memcache, because it's faster and requests there are not taken into account.
PS Nick's Blog - ReferenceProperty prefetching in App Engine - great GAE stuff by Nick Johnson

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question