在Google App Engine中使用祖先或引用属性? [英] Using ancestors or reference properties in Google App Engine?

查看:87
本文介绍了在Google App Engine中使用祖先或引用属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我的很多代码都广泛使用祖先来放置和获取对象。不过,我期待改变一些东西。



我最初认为,如果您知道您所寻找的实体的祖先是谁,祖先可以更快地查询。但我认为事实证明,祖先对交易支持最有用。我没有利用交易,所以我想知道在这里,祖先对系统的负担是否比帮助更重要。

我拥有的是一个用户实体,还有很多其他实体,比如评论,标签,朋友。用户可以创建许多评论,标签和好友,所以无论何时用户这样做,我都将所有这些新创建对象的祖先设置为用户。



所以当我创建评论时,我将祖先设置为用户:

  comment =注释(aUser,key_name = commentId)

现在,我这样做的唯一原因仅限于查询目的。我认为这会更快,当我想让某个用户的所有评论得到所有的评论时,只需要一个共同的祖先,而不是查询authorEmail = userEmail的所有评论。



<因此,当我想获得某个用户的所有评论时,我会这样做:

  commentQuery = db.GqlQuery('SELECT * FROM Comment WHERE ANCESTOR IS:1',userKey)

所以我的问题是,这是一个很好的使用祖先?每个评论是否应该有一个引用创建评论的用户对象的ReferenceProperty,并通过它来过滤?



(另外,我的想法是使用祖先而不是索引ReferenceProperty会节省写入成本,我误解了吗?)

解决方案

你的写作成本是正确的,祖先是一部分免费的关键。如果引用属性被编入索引,使用引用属性会增加写入成本。

由于您在引用属性上查询是否需要编制索引。


祖先不仅对于事务很重要,如果你不用相同的祖先创建每个评论,HRD(默认数据存储实现)中的quires将不会非常一致。

- 添加Nick的评论---

具有相同父代的每个实体将位于同一个实体组中,并写入实体组是被序列化的,所以如果你正在同时编写多个实体,那么在这里使用祖先会减慢速度。由于组中的所有实体都是由组成用户的所有者组成的,因此这不应该成为问题 - 事实上,您所做的实际上是推荐的设计模式。


Currently, a lot of my code makes extensive use of ancestors to put and fetch objects. However, I'm looking to change some stuff around.

I initially thought that ancestors helped make querying faster if you knew who the ancestor of the entity you're looking for was. But I think it turns out that ancestors are mostly useful for transaction support. I don't make use of transactions, so I'm wondering if ancestors are more of a burden on the system here than a help.

What I have is a User entity, and a lot of other entities such as say Comments, Tags, Friends. A User can create many Comments, Tags, and Friends, and so whenever a user does so, I set the ancestor for all these newly created objects as the User.

So when I create a Comment, I set the ancestor as the user:

comment = Comment(aUser, key_name = commentId)

Now the only reason I'm doing this is strictly for querying purposes. I thought it would be faster when I wanted to get all comments by a certain user to just get all comments with a common ancestor rather than querying for all comments where authorEmail = userEmail.

So when I want to get all comments by a certain user, I do:

commentQuery = db.GqlQuery('SELECT * FROM Comment WHERE ANCESTOR IS :1', userKey)

So my question is, is this a good use of ancestors? Should each Comment instead have a ReferenceProperty that references the User object that created the comment, and filter by that?

(Also, my thinking was that using ancestors instead of an indexed ReferenceProperty would save on write costs. Am I mistaken here?)

解决方案

You are right about the writing cost, an ancestor is part of the key which comes "free". using a reference property will increase your writing cost if the reference property is indexed.
Since you query on that reference property if will need to be indexed.

Ancestor is not only important for transactions, in the HRD (the default datastore implementation) if you don't create each comment with the same ancestor, the quires will not be strongly consistent.

-- Adding Nick's comment ---
Every entity with the same parent will be in the same entity group, and writes to entity groups are serialized, so using ancestors here will slow things down iff you're writing multiple entities concurrently. Since all the entities in a group are 'owned' by the user that forms the root of the group in your instance, though, this shouldn't be a problem - and in fact, what you're doing is actually a recommended design pattern.

这篇关于在Google App Engine中使用祖先或引用属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆