Querying Selected Fields In An Entity In Objectify - Appengine
Solution 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.
Solution 2:
If you are looking for projection queries, they are not yet implemented in Objectify.
https://groups.google.com/forum/#!topic/objectify-appengine/uvLIHhHMEM0
Post a Comment for "Querying Selected Fields In An Entity In Objectify - Appengine"