3
votes

Many times I need only the value of one entity's attribute. So it's not necessary to get the whole Entity with key.get().

I know that projection queries returns incomplete entities with the attributes you specify. The values are in the index, so it's not necessary to get the entity from the datastore.

I'm wondering if given a key, there is a way to get a partial entity, getting the information from the index instead of the datastore, because I assume that getting from the index would be faster than from the datastore.

For example something like: article = some_key.get(projection=[Article.title]) print article.title

There is a way to do that? It could be a new feature to ask?

EDIT: It seems there is no way. The existing indexes are ordered by property. The only way is if there is a way to create an index ordered by key, with the property in one column, but it seems it's not very useful to have those extra indexes.

1
You are correct there is no way.Tim Hoffman
Thank you for the confirmation. Trying a little, I could add a property field saving the value of the key of the current entity, so it is saved on the index and ordered by key, but I think it doesn't worth it. In this case the Index would be in the form: | key_property | title | key |Hernán Acosta

1 Answers

3
votes

Without other information this smells highly of premature optimization. Unless you think the potential difference in time represents the bulk of the time in processing you could optimize, you're working in the wrong area.

As an additional note, the bulk of latency in getting an entity is network latency unrelated to the size of an entity. Unless you're working with large entities (xx-xxxx KiB), data retrieval size will be a trivial (if not less) impact on latency.

Having said that, if you do have large entities and really want to try it out, there is a work around to effectively do what you ask while still being strongly consistent.

Example: Let's assume you want to retrieve properties myField1 & myField2 from Kind myKind.

You'll need this index:

- kind: myKind
  ancestor: yes
  properties:
  - name: myField1
  - name: myField2

Assume the id of the entity is myId, and the entity has no descendants, it can be done with the query:

select myField1, myField2 from myKind WHERE __key__ HAS ANCESTOR Key(myKind, myId)

Note: This can be done on child entities, the only requirement is not having descendants of the same kind.

enter image description here

This works because the HAS ANCESTOR operator will also match the ancestor, which in this case is the entity you are looking for. Using HAS ANCESTOR also forces the query to be strongly consistent, meaning it will then behave just like a get which is also strongly consistent.

A downside of this approach is it requires a composite index that includes ancestors - this will require more storage. The other downside is it quite possibly isn't faster. YMMV.