为什么只有关键字的查询是免费的? [英] Why are keys-only queries free?

查看:89
本文介绍了为什么只有关键字的查询是免费的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据定价文档此处键仅限 code>查询是免费的。所以看起来你可以通过执行以下操作来保存读取(伪代码):

  qo = ndb.QueryOptions(keys_only = True)
qry = ModelName.query()。filter(name=Bob)
keys = qry.fetch(20,options = qo)#keys-only fetch

然后我可以得到我的实体,每个实体花费1读取:

  entities = ndb.get_multi(keys)

这是否被认为比获取实体更好?(例如,仅执行按键提取)?

另外,查询费用来自哪里?当执行 fetch 或创建查询对象时

解决方案

这不被认为是更好,你只需为每个查询节省1个额外的读取成本。这是以带有计费实例时间的额外RPC请求为代价的。



执行普通查询(即非密钥查询)时,会返回您正在查找的对象。运行查询的RPC请求也将开始提取查询匹配的每个对象,因此您可以更快地获得所有结果。根据查询的结果大小,它可以全部在一个RPC请求中获取(您可以调整 batch_size 来调整查询所需的RPC总量)。



查询的工作方式是通过一个RPC请求扫描与您的查询匹配的所有键的索引,并且如果其键仅查询,则返回结果列表。如果它是一个普通的查询,它将开始抓取你想要的对象,并执行额外的RPC请求,以根据需要得到它们。



执行查询时收费,所以在你的例子中,当调用 fetch 时。您只需预先建立查询和过滤器即可。



尽管如此,仍然有一个优势:可以始终假定提取的对象是一致的。也就是说,对于查询,您的结果不能保证与任何刚刚发生的更新一致。这两种方法的最终一致性在于,与查询匹配的对象可能不会全部从索引中一致地读取,除非您执行祖先查询,但是您的方法可以保证您使用其键获取的对象确实是最多的到目前为止的版本。


According to pricing docs here, a keys-only query is free. So it seems you can save a read by doing the following (pseudo-code):

qo = ndb.QueryOptions(keys_only = True)
qry = ModelName.query().filter("name" = "Bob")
keys = qry.fetch(20, options = qo) #keys-only fetch

Then afterward I can get my entities, costing 1 read per entity:

entities = ndb.get_multi(keys)

Why is this considered better than fetching the entities (i.e. doing a keys-only fetch)?

Also, where does the query charge come from? When the fetch is performed or when the query object is created?

解决方案

This isn't considered "better", you just save yourself 1 extra read cost per query. This comes at the cost of an extra RPC request with billable instance time.

When you do a normal query (i.e. a non keys-only query) you are returned the objects you are looking for. The RPC request that runs the query will also start to fetch each object the query matches and thus you can get all your results faster. Depending on the result size of the query, it can all be fetched in a single RPC request (you can adjust batch_size to adjust the total amount of RPCs a query takes).

The way the query works is by a single RPC request scanning your indexes for all keys that match your query and if its a keys only query, it returns just the resulting list. If its a normal query, it will start to grab the objects you want and perform additional RPC requests to get them all as needed.

The charge comes when the query is executed, so in your example, when fetch is called. You are just building up the query and its filters beforehand.

There is an advantage to doing it your way though: The fetched objects can always be assumed to be consistent. That is, with a query your result is not guaranteed to be "consistent" with any updates that may have just occurred. Both methods will suffer from eventual consistency in that the objects that match your query might not all be read consistently from the indexes unless you do an ancestor query, but your method can guarantee that the objects you fetch by using their keys are indeed the most up-to-date versions.

这篇关于为什么只有关键字的查询是免费的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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