Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need a way to count entities in GAE datastore that meet a certain condition? (over 1000 entities)

I'm building an app on GAE that needs to report on events occurring. An event has a type and I also need to report by event type.

For example, say there is an event A, B and C. They occur periodically at random. User logs in and creates a set of entities to which those events can be attributed. When the user comes back to check the status, I need to be able to tell how many events of A, B and/or C occurred during a specific time range, say a day or a month.

The 1000 limit is throwing a wrench into how I would normally do it. I don't need to retrieve all of the entities and present them to the user, but I do need to show the total count for a specific date range. Any suggestions?

I'm a bit of python/GAE noob...

like image 427
Linsidious Avatar asked Jan 23 '23 15:01

Linsidious


2 Answers

App Engine is not a relational database and you won't be able to quickly do counts on the fly like this. The best approach is to update the counts at write time, not generate them at read time.

When generating counts, there are two general approaches that scale well with App Engine to minimize write contention:

  1. Store the count in Memcache or local memory and periodically flush. This is the simplest solution, but it can be volatile and data loss is probable.
  2. Use a Sharded Counter. This approach is a bit more reliable but more complex. You won't be able to sort easily by count, but you could also periodically flush to another indexed count field periodically and sort by that.
like image 77
Ikai Lan Avatar answered Jan 25 '23 04:01

Ikai Lan


Results of datastore count() queries and offsets for all datastore queries are no longer capped at 1000.

Since Version 1.3.6

like image 39
systempuntoout Avatar answered Jan 25 '23 03:01

systempuntoout