Reputation: 13
I am using objectify v6. I want to get a count of all entities in my datastore. It looks like there is a limit on look up operation in datastore https://cloud.google.com/datastore/docs/concepts/limits.
In my datastore, I have 2000 entities, when I do
ObjectifyService.ofy().load().type(MyEntity.class).keys();
this works, gives no error, but when I do
ObjectifyService.ofy().load().type(MyEntity.class).list()
this gives me error saying there is a limit on query, does that mean I can get a count of all entities by counting all the keys?
Upvotes: 1
Views: 446
Reputation: 13556
Sure, you can count all the entities in your kind by using keys()
... right up until you run into the same problem, which is that your query takes too long. I don't know how far you can get but I doubt you'll reach millions. This approach will probably only perform reasonably for thousands.
There isn't a good equivalent of SELECT COUNT(*)
in the datastore. You can track adds and removes in a sharded counter if you need an up-to-date answer. If you only need an approximate, you can use map/reduce (see the __scatter__
property) or you can just look at the statistics that Google periodically recomputes:
https://cloud.google.com/datastore/docs/concepts/stats
Upvotes: 1