实体子级的 Google App Engine 查询(而非过滤器)
实体的子实体在查询中是否可用?
假设:
class Factory(db.Model):
""" Parent-kind """
name = db.StringProperty()
class Product(db.Model):
""" Child kind, use Product(parent=factory) to make """
@property
def factory(self):
return self.parent()
serial = db.IntegerProperty()
假设 500 家工厂生产 500 种产品,总共 250,000 种产品。 有没有一种方法可以形成资源高效的查询,仅返回某个特定工厂生产的 500 种产品? 祖先方法是一个过滤器,因此使用例如 Product.all().ancestor(factory_1) 需要重复调用数据存储区。
Are the children of an entity available in a Query?
Given:
class Factory(db.Model):
""" Parent-kind """
name = db.StringProperty()
class Product(db.Model):
""" Child kind, use Product(parent=factory) to make """
@property
def factory(self):
return self.parent()
serial = db.IntegerProperty()
Assume 500 factories have made 500 products for a total of 250,000 products. Is there a way to form a resource-efficient query that will return just the 500 products made by one particular factory? The ancestor method is a filter, so using e.g. Product.all().ancestor(factory_1) would require repeated calls to the datastore.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尽管祖先被描述为“过滤器”,但它实际上只是更新查询以添加祖先条件。 在迭代查询之前,您不会向数据存储区发送请求,因此您所拥有的将正常工作。
但有一点是:具有相同父级的 500 个实体可能会损害可扩展性,因为写入会序列化到实体组的成员。 如果您只想跟踪制造产品的工厂,请使用 ReferenceProperty:
然后您可以使用以下方法获取所有产品:
Although ancestor is described as a "filter", it actually just updates the query to add the ancestor condition. You don't send a request to the datastore until you iterate over the query, so what you have will work fine.
One minor point though: 500 entities with the same parent can hurt scalability, since writes are serialized to members of an entity group. If you just want to track the factory that made a product, use a ReferenceProperty:
You can then get all the products by using: