Querying selected fields in an entity in objectify - appengine

孤街浪徒 提交于 2019-12-31 05:15:11

问题


I am using appengine and objectify as a backend for my app. And when i query in the datastore i get a Entity object which has the data of required row. But, using objectify how will i query entities and get oly selected fileds from it? because querying the whole entity will be heavy and it needs more data bandwidth.

Eg : In a entity with 4 columns, --> Id,name,description,age. I should query oly Id,name,age. I dont want description to be queried.


回答1:


The GAE datastore does not work like an RDBMS; you can't arbitrarily pick and choose which fields to query out of an entity. The standard behavior of a datastore query is to follow an index (which maps attribute value to entity key), then fetch-by-key all the entities found.

There is a feature called "projection queries" (which Objectify supports; look for the project() method on the query command object), however it is not a general purpose SELECT statement like you get in SQL. Projection queries capitalize on the fact that the index itself contains the index values, so if you only want data that's in the index, you don't need to perform a subsequent fetch of the whole Entity. However, this comes with some restrictions:

  • You must maintain a multiproperty index with all the data you wish to project.
  • You must maintain single-property indexes for each of the fields in the multiproperty index.
  • You can only project on queries that follow that particular index.
  • Queries bypass Objectify's memcache-based entity cache.

Be aware of the cost of using projection queries. In your example, you will need single-property indexes on Name and Age plus a multiproperty index on {__key__, Name, Age}. Instead of 3 write operations per entity written, your new entity will cost 8 write ops. On the other hand, the cost of a projection query is a constant 1 read op.

On the other other hand, the cost of a batch get from memcache is 0, and the worst-case cost is 1 read op. Unless your description field is known to be causing you problems, this is a massive premature optimization.




回答2:


If you are looking for projection queries, they are not yet implemented in Objectify.

https://groups.google.com/forum/#!topic/objectify-appengine/uvLIHhHMEM0



来源:https://stackoverflow.com/questions/24862936/querying-selected-fields-in-an-entity-in-objectify-appengine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!