Friday, April 29, 2011

how does one get a count of rows in a datastore model in google appengine?

I need to get a count of records for a particular Model on app engine. How does one do it?

I bulk uploaded more than 4000 records but modelname.count() only shows me 1000.

From stackoverflow
  • As of release 1.3.6, there is no longer a cap of 1,000 on count queries. Thus you can do the following to get a count beyond 1,000:

    count = modelname.all(keys_only=True).count()
    

    This will count all of your entities, which could be rather slow if you have a large number of entities. As a result, you should consider calling count() with some limit specified:

    count = modelname.all(keys_only=True).count(some_upper_bound_suitable_for_you)
    
    a paid nerd : Of course, you only have 300ms to do this :(
    Nick Johnson : 300ms? Where did you get that figure? Regardless, though, this demonstrates why counting objects on the fly is not a good idea.
    dar : My guess is he meant 30,000ms. But that really isn't the case because if you're doing this because of the bulk uploader, you probably just run the count over the remote_api anyway - which AFAIK is not subject to the 30 second timeout.
    Shay Erlichmen : I added keys_only=True which is faster
  • In GAE a count will always make you page through the results when you have more than 1000 objects. The easiest way to deal with this problem is to add a counter property to your model or to a different counters table and update it every time you create a new object.

0 comments:

Post a Comment