【问题标题】:Ordering a query订购查询
【发布时间】:2017-02-14 20:49:42
【问题描述】:

下面的这组查询将一堆配置文件连接在一起以返回到网站。

我想对这些进行一次查询,因为我想对整个配置文件集合使用 order 函数。

# Get all users for who 'currentUser' is an assistant for.
users = Users.query(Users.assistant.IN(currentUser)).fetch()

# Get all profiles that belong to those users and have not been deleted.
for user in users:
    profiles = profiles + Profiles.query(
            ndb.AND(
            Profiles.user == user.username,
            Profiles.deleted == False
        )
    ).order(Profiles.history).fetch() # Order all profiles by timestamp

【问题讨论】:

    标签: python google-cloud-datastore app-engine-ndb datastore


    【解决方案1】:

    您正在寻找的内容在某种程度上相当于:

    # get the list of usernames obtained above
    usernames = [user.username for user in users]
    
    profiles = Profiles.query(ndb.AND(Profiles.user.IN(usernames),
                                      Profiles.deleted == False))
                             .order(Profiles.history).fetch()
    

    但是,如果usernames 包含许多元素(显然超过 10 个左右),这将产生性能问题,请参阅Does the NDB membership query ("IN" operation) performance degrade with lots of possible values?。

    【讨论】:

      猜你喜欢
      • 2017-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-17
      • 1970-01-01
      • 2016-12-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多