Using "ANCESTOR IS" to improve datastore performance?

65 views
Skip to first unread message

heavyz

unread,
Nov 10, 2008, 5:23:04 AM11/10/08
to Google App Engine
Hi,

As described in the GAE document: "Every entity with a given root
entity as an ancestor is in the
same entity group. All entities in a group are stored in the same
datastore node." Imagine that I
have two model classes: Foo and CommentOnFoo:

class Foo(db.Model):
# some properties...

class CommentOnFoo(db.Model):
foo = db.ReferenceProperty(Foo)
# some other properties...

In my webapp, when I create a CommentOnFoo entity, I always associate
a Foo entity as its parent.
Now I have some questions about the ancestor relationship between Foo
and CommentOnFoo...

1. Does it mean that searching entities in the same datastore node is
faster? If yes, to make the
query faster, shall I always use the 'ANCESTOR IS' condition? For
example, given a Foo entity
"foo", to retrieve all the comments on it, I can use the following
GQLs:

#1: comments = CommentOnFoo.gql("WHERE foo=:foo", foo=foo)
#2: comments = CommentOnFoo.gql("WHERE ANCESTOR IS :foo AND foo=:foo,
foo=foo)

Will the #2 query be faster than #1 (since #2 will be run in a single
datastore node)?

2. Because the ancestor relationship also establishes a one-to-many
relationship, should I remove
CommentOnFoo's "foo" property (ReferenceProperty to Foo)? I think it's
redundant.

PS. I know that I should not use ancestor just to make one-to-many
relationship. I used ancestor as
it's a requirement of transaction.

BTW: I will greatly appreciate if Google could publish a "GAE best
practice" article...

Thanks & regards.


Rodrigo Moraes

unread,
Nov 10, 2008, 7:16:04 AM11/10/08
to google-a...@googlegroups.com
On Mon, Nov 10, 2008 at 8:23 AM, heavyz wrote:
> 2. Because the ancestor relationship also establishes a one-to-many
> relationship, should I remove
> CommentOnFoo's "foo" property (ReferenceProperty to Foo)? I think it's
> redundant.

since you define Foo as parent of CommentOnFoo, it'd be redundant
indeed to have it as a property.

regarding performance, the docs says: "Entity groups do not have a
significant impact on the speed of queries.", but "improve the
performance of creating and updating data". [1]

[1] http://code.google.com/appengine/docs/datastore/keysandentitygroups.html#Entity_Groups_Ancestors_and_Paths

-- rodrigo

heavyz

unread,
Nov 10, 2008, 9:21:53 AM11/10/08
to Google App Engine
Reply all
Reply to author
Forward
0 new messages