GQL查询有效地跨越实体关系 [英] GQL query to effectively span entity relationships

查看:91
本文介绍了GQL查询有效地跨越实体关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我处于需要跨越Google App Engine实体关系的情况,例如在Django数据库模型中。我使用 ListProperty 来实现一对多关系,如下所示:

I'm in a situation where I need to span Google App Engine entity relationships in a query like in the Django database model. I'm using ListPropertys for one-to-many relationships, like so:

class Foo(db.Model): bars = db.ListProperty(db.Key)
class Bar(db.Model): eggs = db.ListProperty(db.Key)

我想执行以下查询:

And I'd like to perform a query that does the following:

# Foo.filter('bars.eggs =', target_egg)
[foo
for egg in eggs if egg == target_egg
for eggs in bar.eggs
for bar in foo.bars
for foo in Foo.all()]

理解似乎根本没有效率。我真的想在注释部分执行查询,但它看起来不像 GQL语法允许查询属性的属性:

The comprehension seems radically inefficient. I'd really like to perform a query as in the commented out portion, but it doesn't look like the GQL syntax allows for queries against the attributes of attributes:

   SELECT * FROM <kind>
    [WHERE <condition> [AND <condition> ...]]
    [ORDER BY <property> [ASC | DESC] [, <property> [ASC | DESC] ...]]
    [LIMIT [<offset>,]<count>]
    [OFFSET <offset>]

  <condition> := <property> {< | <= | > | >= | = | != } <value>
  <condition> := <property> IN <list>
  <condition> := ANCESTOR IS <entity or key>


推荐答案

您是对的,App Engine数据存储区不'允许这种查询。你说得对,列表理解是无效的。不过考虑一下,当你用像你这样的连接执行一个查询时,这与关系数据库几乎完全相同 - 数据库必须在这里执行相同的O(n ^ 3)工作 - 唯一的区别是你是用Python做的,还有额外的往返时间。由于App Engine的设计是为了扩展,所以它并不是真正用于这类查询。

You're right, the App Engine datastore doesn't allow for this sort of query. And you're right that the list comprehension is inefficient. Consider, though, that that is pretty much exactly what a relational database does when you execute a query with joins like your one - the database has to perform the same O(n^3) work you're doing here - the only difference is that you're doing it in Python, and with additional round trip time. Since App Engine is designed to scale, it's not really made for these sort of queries.

然而,通常有一种方法可以对模型进行非规范化处理,以便于实现通过将您需要访问的某些属性移动到Foo模型上,或者如果您正在执行聚合,请将总数移动到Foo模型上。虽然没有更多关于你想要解决什么问题的想法,但很难提供具体的解决方案。

Usually, though, there's a way you can denormalise your model a bit to facilitate this, by moving some of the properties you need to access onto the Foo model, or if you are doing aggregates, by moving the total onto the Foo model. It's difficult to give concrete solutions without more of an idea of what problem you're trying to solve, though.

这篇关于GQL查询有效地跨越实体关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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