For 100k+ entities in google datastore, ndb.query().count() is going to cancelled by deadline , even with index. I’ve tried with produce_cursors options but only iter() or fetch_page() will returns cursor but count() doesn’t.
How can I count large entities?
To do something that expensive you should take a look on Task Queue Python API. Based on the Task Queue API, Google App Engine provides the deferred library, which we can use to simplify the whole process of running background tasks.
Here is an example of how you could use the deferred library in your app:
Then you can call the above function from within your app like:
While I’m still not sure if the
count()going to return any results with such large datastore you could use thiscount_large_query()function instead, which is using cursors (untested):To try locally the above set the
LIMITto 4 and check if in your console you can see theTotal entitites: ##line.As Guido mentioned in the comment this will not going to scale either:
So you might want to take a look on best practices for writing scalable applications and especially the sharding counters.