简体   繁体   English

存在的GQL查询:获取与密钥查询与实体查询

[英]GQL Query for Existence: Get vs. Key Query vs. Entity Query

While the Google Cloud Client Library has a lot of built-in functionality, I haven't found anything to use when querying for an Entitys existence based on its id. 虽然Google Cloud Client Library具有许多内置功能,但在查询基于其ID的Entity存在时,我找不到任何可用的内容。 From what I understand, it is much more efficient to make a key query, than an entity query like so. 根据我的理解,创建一个关键查询比这样的实体查询更有效。

SELECT __key__ FROM User WHERE __key__ = Key(User, 1)

If possible from within a Java app, using the get(Key key) function would be even better. 如果可能,在Java应用程序中,使用get(Key key)函数会更好。

Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
KeyFactory keyFactory = datastore.newKeyFactory().setKind(User);
Key key = keyFactory.newKey(1);
Entity entity = datastore.get(key);

If I have an instance where my Entity has ancestors as well, which would be more efficient of the three when querying for the existence of an ancestor with just the parent ID? 如果我有一个实例,我的实体也有祖先,当查询只有父ID的祖先的存在时,这三个会更有效吗? I assume that the entity query ( SELECT * From Parent WHERE __key__ = Key(Parent, 1) ) will always be slower than a key query, but how about when compared to the get? 我假设实体查询( SELECT * From Parent WHERE __key__ = Key(Parent, 1) )总是比关键查询慢,但是当与get相比时呢?

What about when the ancestor paths are chained even longer? 什么时候祖先的路径链接更长? Then the key queries may become something along the likes of: 然后,关键查询可能变成以下类似的东西:

SELECT __key__ FROM Grandchild WHERE __key__ = Key(Parent, 1, Child, 1, Grandchild, 1)

Key-only queries are faster because the Entity data does not have to be transmitted back to the client. 仅密钥查询更快,因为实体数据不必传输回客户端。 Let's say you have an entity with N number of properties and the size of those properties (names and values) is 50 KB, you are saving 50 KB of data transfer over the network, if you do a key-only query. 假设您有一个具有N个属性的实体,并且这些属性(名称和值)的大小为50 KB,如果您执行仅密钥查询,则通过网络可以节省50 KB的数据传输。

The actual lookup of the entity from the Datastore point of view would be same whether you use Key-only query, Entity Query based on a Key or Get(key) method because the Datastore just looks up the entity based on the provided key. 无论您使用仅基于密钥的查询,基于密钥还是Get(密钥)方法的实体查询,从数据存储的角度实际查找实体都是相同的,因为数据存储区只是根据提供的密钥查找实体。

To summarize - if all you need to know is if an entity with the given keys exists are not, key-only query would be your best option. 总结一下 - 如果您需要知道的是,是否存在具有给定密钥的实体,则仅密钥查询将是您的最佳选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM