views:

45

answers:

2

Hi,

I know keeping global counters is frowned upon in app engine. I am interested in getting some stats though, like once every 24 hours. For example, I'd like to count the number of User objects in the system once every 24 hours.

So how do we do this? Do we simply keep a set of admin tool functions which do something like:

SELECT FROM com.me.project.server.User;

and just see what the size of the returned List is? This is kind of a bummer because the datastore would have to deserialize every single User instance to create the returned list, right?

I could optimize this possibly by asking for only the keys to be returned, so the whole User object doesn't have to be deserialized.

Then again, a global counter for # of users probably would create too much contention, because there probably won't be hundreds of signups a minute for the service I'm creating.

How should we go about doing this? Getting my total number of users once a day is probably a pretty typical operation?

Thank you

A: 

I wouldn't bother keeping a running count if you only care about checking it once per day.

User.all().count() will work until you reach 1000 users (count() is limited to 1000 entities)

For queries for more than 1000 users, you can use a cursor, querying for 1000 at a time using a cursor to start the next query where you left off, and stopping when the resulting count is less than 1000.

Jason Hall
Awesome thanks, yeah I think the stats method listed by dound will work well, but if I want to do something like "# of users in New York", then I'd be likely to use the cursor method to do the specific search and page through the results until I find the maximum. Thanks.
+3  A: 

You can use the datastore statistics API to determine how many user objects there are too. This might be easier (and faster) than using cursors if you have more than 1,000 users.

David Underhill
Alright this seems like the easiest way to go if I want just a global count, thank you!