10

I am storing a key of an entity as a property of another in order to relate them. We are in a refactor stage at this point in the project so I was thinking about introducing ancestors. Is there a performance difference between the two approaches? Any given advantages that I might gain if we introduce ancestors?

class Book(ndb.Model):
  ...

class Article(ndb.Model):
  book_key = ndb.KeyProperty(kind=Book, required=True)


book_key =  ndb.Key("Book", 12345)

1st ancestor query approach

qry = Article.query(ancestor=book_key)

2st simple key query approach

qry = Article.query(book_key=book_key)
Ahmad Khan
  • 2,655
  • 19
  • 25
topless
  • 8,069
  • 11
  • 57
  • 86

1 Answers1

15

The ancestor query will always be fully consistent. Querying by book_key, on the other hand, will not necessarily be consistent: you may find that recent changes will not be shown in that query.

On the other hand, introducing an ancestor imposes a limit on the number of updates: you can only do one update per second to any entity group (ie the ancestor and its children).

It's a trade-off for you as to which one is more important in your app.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • I didn't see that limit (1 update/entity group/second) in the docs -- can you provide a link? Something of a scary quota! – rattray Aug 14 '13 at 03:46
  • 2
    You should expect 1 write per second, though it could be slightly more in practice. I wasn't able to find a table with this number, but take a look at the last paragraph on https://developers.google.com/appengine/docs/python/datastore/structuring_for_strong_consistency and more discussion here https://groups.google.com/forum/#!topic/google-appengine/llEXU-B3dQ4 – JJ Geewax Sep 22 '13 at 13:59