1

Let's say I have several entities stored in the AppEngine DataStore that all have an indexed numerical property and I need to find the largest currently existing value.

I could simply do a projection query sorted on this property with descending sort-order and a limit of 1. The returned result would then contain my answer.

Is this the best approach or is there a more efficient way to get this value?

(Consistency is not too much of a worry in this case. If I miss a couple values that are being committed simultaneously, I don't care too much. So, no need for transactions here.)

Charles
  • 50,943
  • 13
  • 104
  • 142
Markus A.
  • 12,349
  • 8
  • 52
  • 116

1 Answers1

2

Since GAE datastore does not have an equivalent to MAX on an SQL database, it is indeed best to order by the column and retrieve the first entry. For example:

max_val = MyEntity.query().order(-MyEntity.my_numeric_field).get().my_numeric_field

See also this forum discussion.

Your second option would be to store the maximum value in some other entity and then update it (probably in a transaction) whenever you add one of your first entities.

dlebech
  • 1,817
  • 14
  • 27
  • Since I already have the index, I will go with the query then. +1 for the link and pointing out the (in some scenarios a lot more efficient) option of keeping an updated max-value stored in a separate location. Maybe even in MemCache. – Markus A. Aug 16 '13 at 16:22
  • The problem with MemCache is of course that data can expire at any time. However, if you use the `ndb` datastore, you do not even have to worry about MemCache because GAE will automatically use it when appropriate. – dlebech Aug 20 '13 at 07:24