投影查询与常规查询 [英] Projection Queries vs. Regular Queries

查看:80
本文介绍了投影查询与常规查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读Cloud Datastore上的文档后,很明显Get会检索整个实体,并且查询总是针对索引进行的.

Reading the documentation on Cloud Datastore, it's pretty clear that a Get retrieves the entire entity, and that a query is always done against the index.

这是令人困惑的地方,因为根据文档:

Here's where it gets confusing, because according to the documentation:

投影查询可让您仅查询Cloud Datastore的那些查询您实际需要的实体的特定属性,较低延迟和成本,而不是检索整个实体

Projection queries allow you to query Cloud Datastore for just those specific properties of an entity that you actually need, at lower latency and cost than retrieving the entire entity

但是,如果所有查询都是针对索引进行的,那么查询将永远不会检索整个实体,因为一切都是从索引中获取的?如果我有一个具有3个属性(A,B和C)的索引,则使用常规查询或投影查询应该没有什么区别,因为我不能只投影" A和B,因为索引还包含C,因此在此示例中,我将不得不投影"与常规查询相同的属性?

But if all queries are done against an index, a query would never retrieve the entire entity anyway as everything is taken from the index? If I have an index with 3 properties (A, B and C) there shouldn't be any difference in using a regular query or a projection query, because I can't "project" just A and B, as the index also contains C, so in this example I would have to "project" the same properties as the regular query?

在我看来,仅当索引不包含实体的所有属性时,才可以真正使用投影查询吗?因此,如果一个实体没有很多属性,那么使用投影就没有任何意义,因为大多数(如果不是全部)查询无论如何都会使用/获取所有属性?我想知道在这种情况下使用投影是否在成本上合理,因为文档说投影具有更低的延迟和成本,但是在获取所有属性时是否适用于投影?

It seems to me that projection queries can only really be used when an index doesn't contain all the properties on an entity? So if an entity doesn't have a lot of properties, it doesn't make any sense to use projection because most (if not all) queries will be using/getting all the properties anyway? I'm wondering if makes sense cost-wise to use projection in this case, because the documentation says that projection has lower latency and cost but would that apply to projection when grabbing all properties?

推荐答案

每个

Per this article (probably the same article you're looking at), Projection queries require all properties specified in the projection to be included in a Cloud Datastore index.

针对Cloud Datastore的常规"(我指的是 SELECT * ... )查询通常使用仅包含所查询实体属性的排序子集的索引,以及指向完整实体的指针,投影查询针对包含查询所请求的所有字段的索引运行.因此,似乎可以通过索引识别出与查询匹配的实体集,从而消除了对获取查询的实体的需求,从而显着增加了延迟.

Whereas "regular" (which I'm taking to mean SELECT * ...) queries against Cloud Datastore typically use indexes that only contain a sorted subset of the properties of the queried entities, plus pointers to the full entities, projection queries run against indexes that contain all the fields requested by the query. So it appears the significant latency gain comes from the elimination of the need to fetch the queried entities once the set of entities matching the query has been discerned via the index.

因此,当您编写时,如果所有查询都是针对索引进行的,那么查询将永远不会检索整个实体,因为一切都是从索引中获取的?,这是不准确的:非投影查询是前往:

So when you write if all queries are done against an index, a query would never retrieve the entire entity anyway as everything is taken from the index?, that's not accurate: Non-projection queries are going to:

  1. 确定有效完成查询所需的索引(如果该索引不存在,则会引发异常)
  2. 使用它来获取指向匹配实体的指针,并且
  3. 获取那些匹配的实体

据我所知,投射查询是Cloud Datastore提供的仅使用索引来实现查询的唯一机制(没有上面的步骤3).

As far as I can tell, projection queries are the only mechanism Cloud Datastore provides for fulfilling a query using only an index (without step #3 above).

我还没有阅读过任何文档,即使您确实配置了包含特定数据存储类型的所有实体中所有属性的索引(这是不寻常的),查询引擎还是足够聪明"的,可以使用该索引即使在技术上可能的情况下也可以执行上述第3步的查询.实际上,由于Cloud Datastore是无模式的,并且相同类型的实体可以具有不同的属性,所以即使知道索引是否包含给定实体的所有属性而不获取该实体,也将比使用a实体涉及更多的任务.具有模式的数据库.

I haven't read any documentation suggesting that, even if you do configure an index containing all the properties across all entities of a particular datastore kind (which would be unusual), the query engine would be "smart" enough to use that index to fulfill queries without step #3 above even where technically possible. Indeed, since Cloud Datastore is schemaless and entities of the same kind can have different properties, even knowing whether or not an index contains all the properties for a given entity without fetching that entity would be a much more involved task than it would be with a database with schema.

这篇关于投影查询与常规查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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