问题
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