ndb.OR 使查询成本更高 [英] ndb.OR makes query costs more

查看:34
本文介绍了ndb.OR 使查询成本更高的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 AppEngine appstats 我分析了我的查询,并注意到虽然文档说查询需要一次读取,但使用 ndb.OR(或扩展为 OR 的 .IN)的查询需要花费 n 次读取(n 等于 OR 子句的数量).

Using AppEngine appstats I profiled my queries, and noticed that although the docs say a query costs one read, queries using ndb.OR (or .IN which expands to OR), cost n reads (n equals the number of OR clauses).

例如:

votes = (Vote.query(ndb.OR(Vote.object == keys[0], Vote.object == keys[1]))
     .filter(Vote.user_id == user_id)
     .fetch(keys_only=True))

此查询需要 2 次读取(匹配 0 个实体).如果我将 ndb.OR 替换为 Vote.object.IN,则读取次数等于我传递给读取的数组长度.

This query costs 2 reads (it matches 0 entities). If I replace the ndb.OR with Vote.object.IN, the number of reads equals the length of array I pass to read.

这种行为与文档有些矛盾.

This behavior is kind of contradicts the docs.

我想知道是否有其他人遇到过同样的情况,这是否是 AE、文档或我的理解中的错误.

I was wondering if anyone else experienced the same, and if this is a bug in AE, docs, or my understanding.

谢谢.

推荐答案

ndb 的查询文档不是特别明确,但这一段是你最好的答案

The query docs for ndb are not particularly explicit but this paragraph is your best answer

除了原生操作符之外,API 还支持 != 操作符,使用布尔 OR 运算和 IN 组合过滤器组操作,测试与可能值列表之一的相等性(就像 Python 的in"运算符).这些操作不会 1:1 映射到Datastore 的原生操作;因此它们有点古怪和缓慢,相对地.它们是使用结果的内存合并实现的流.请注意,p != v 被实现为p v".(这个重复属性很重要.)

In addition to the native operators, the API supports the != operator, combining groups of filters using the Boolean OR operation, and the IN operation, which test for equality to one of a list of possible values (like Python's 'in' operator). These operations don't map 1:1 to the Datastore's native operations; thus they are a little quirky and slow, relatively. They are implemented using in-memory merging of result streams. Note that p != v is implemented as "p < v OR p > v". (This matters for repeated properties.)

在本文档中 https://developers.google.com/appengine/docs/python/ndb/queries

这篇关于ndb.OR 使查询成本更高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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