GAE NDB 使用游标对多查询进行排序
GAE NDB Sorting a multiquery with cursors
在我的 GAE 应用程序中,我正在执行一个必须按日期排序的查询。查询必须包含 IN 过滤器,但这会导致以下错误:
BadArgumentError: _MultiQuery with cursors requires __key__ order
现在我已经阅读了其他 SO 问题 (like this one),它建议更改为按键排序(错误也指出)。然而,问题是查询随后变得无用。它需要按日期排序。实现这一目标的建议方法是什么?
云数据存储服务器不支持 IN
。 NDB 客户端库通过将带有 IN
的查询拆分为多个带有相等运算符的单个查询来有效地伪造此功能。然后在客户端合并结果。
由于可以在这些单个查询中的 1 个或多个中返回相同的实体,因此合并这些值在计算上变得很愚蠢*,除非您按键排序**。
相关,你应该阅读underlying caveats/limitations on cursors以获得更好的理解:
- Because the NOT_EQUAL and IN operators are implemented with multiple queries, queries that use them do not support cursors, nor do composite queries constructed with the CompositeFilterOperator.or method.
- Cursors don't always work as expected with a query that uses an inequality filter or a sort order on a property with multiple values. The de-duplication logic for such multiple-valued properties does not persist between retrievals, possibly causing the same result to be returned more than once.
如果 IN
中使用的值列表是静态列表而不是在运行时确定,解决方法是在编写实体时将其计算为索引布尔字段。这允许您使用单个相等过滤器。例如,如果您有一个错误跟踪器并且想要查看未解决问题的列表,您可以对查询使用 IN('new', 'open', 'assigned')
限制。或者,您可以将名为 is_open
的 属性 设置为 True
,这样您就不再需要 IN
条件。
* 计算愚蠢:需要对无限数量的先前值进行线性扫描以确定当前检索到的实体是否重复。也称为概念上与游标不兼容。
** 关键之所以有效,是因为我们可以在不同的单个查询之间交替检索下一组值,而不必担心对整个过程结果集进行线性扫描。这为我们提供了一个可以使用的有界数据集。
在我的 GAE 应用程序中,我正在执行一个必须按日期排序的查询。查询必须包含 IN 过滤器,但这会导致以下错误:
BadArgumentError: _MultiQuery with cursors requires __key__ order
现在我已经阅读了其他 SO 问题 (like this one),它建议更改为按键排序(错误也指出)。然而,问题是查询随后变得无用。它需要按日期排序。实现这一目标的建议方法是什么?
云数据存储服务器不支持 IN
。 NDB 客户端库通过将带有 IN
的查询拆分为多个带有相等运算符的单个查询来有效地伪造此功能。然后在客户端合并结果。
由于可以在这些单个查询中的 1 个或多个中返回相同的实体,因此合并这些值在计算上变得很愚蠢*,除非您按键排序**。
相关,你应该阅读underlying caveats/limitations on cursors以获得更好的理解:
- Because the NOT_EQUAL and IN operators are implemented with multiple queries, queries that use them do not support cursors, nor do composite queries constructed with the CompositeFilterOperator.or method.
- Cursors don't always work as expected with a query that uses an inequality filter or a sort order on a property with multiple values. The de-duplication logic for such multiple-valued properties does not persist between retrievals, possibly causing the same result to be returned more than once.
如果 IN
中使用的值列表是静态列表而不是在运行时确定,解决方法是在编写实体时将其计算为索引布尔字段。这允许您使用单个相等过滤器。例如,如果您有一个错误跟踪器并且想要查看未解决问题的列表,您可以对查询使用 IN('new', 'open', 'assigned')
限制。或者,您可以将名为 is_open
的 属性 设置为 True
,这样您就不再需要 IN
条件。
* 计算愚蠢:需要对无限数量的先前值进行线性扫描以确定当前检索到的实体是否重复。也称为概念上与游标不兼容。
** 关键之所以有效,是因为我们可以在不同的单个查询之间交替检索下一组值,而不必担心对整个过程结果集进行线性扫描。这为我们提供了一个可以使用的有界数据集。