【问题标题】:Query/GqlQuery .order() restricting the result set?Query/GqlQuery .order() 限制结果集?
【发布时间】:2009-11-07 02:41:37
【问题描述】:

我刚刚注意到一个我无法理解的查询的奇怪结果。似乎将 order() 添加到 Query 会限制我返回的结果

这是我的互动:

>>> SomeModel.all().filter('action =', 'foo').order('created_at').count(),
    SomeModel.all().filter('action =', 'foo').count()
(192L, 293L)

>>> SomeModel.all().filter('action =', 'foo').order('created_at').count(),
    SomeModel.all().filter('action =', 'foo').count()
(193L, 294L)

如您所见,两个查询之间没有添加一百个实体。似乎 order() 指令限制了结果集。但created_at 是必需属性,存在于所有实体中。

>>> count = 0
>>> for entity in SomeModel.all().filter('action =', 'foo'):
...   if not entity.created_at:
...     raise Exception, 'Not found!'
...   count += 1
...
>>> print count
361

没有例外。那么为什么使用 ORDER 的查询不会返回所有实体呢?

最后,调查是否是坏数据:

>>> print "ascending=%d no-filter=%d descending=%d" % (
      SomeModel.all().filter('action =', 'foo').order('created_at').count(),
      SomeModel.all().filter('action =', 'foo').count(),
      SomeModel.all().filter('action =', 'foo').order('-created_at').count())
ascending=79 no-filter=179 descending=173

【问题讨论】:

  • 看起来我总是缺少 100 个实体,所以这可能是某种错误的数据。我会更新的。
  • 如果你使用 len(SomeModel.all().filter('action =', 'foo').order('created_at').fetch()) 而不是 count,你还能得到同样的问题?如果是,您可以尝试查看排序正在删除的实体类型。

标签: google-app-engine google-cloud-datastore gql


【解决方案1】:

尽管没有更改我的代码,但问题已经消失。我最好的猜测是索引可能落后了,尽管我假设如果我从 put() 成功返回,那么索引就会更新。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多