开发者

Doing a global count of an object type (like Users), best practice?

开发者 https://www.devze.com 2022-12-27 12:00 出处:网络
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

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 crea开发者_如何学Gote 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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消