开发者

Python Google App Engine: How do you find the count number of a particular entity in the datastore?

I want to be able to give each instance of a particular object a unique number id in the order that they are created, so I was thinking of getting the number of the particular entity already in the datastore and add 1 to 开发者_运维技巧get the new number.

I know I can do something like

query = Object.all()
count = query.count()

but that has some limitations.

Does anybody know a better way to find the number of particular entities or even a better way to give objects a unique sequential number id?

Thanks!


Why do your IDs need to be sequential? The App Engine datastore generates integer IDs for your entities already; they're not guaranteed to be sequential, but they are guaranteed to be unique, and they tend to be small.

The ID generation strategy for App Engine is not 'perfect' - entirely sequential - because doing so in a distributed system is impractical, as it introduces a single bottleneck (the service that hands out IDs). Any system you build will suffer from the same issue, unless you want only a low rate of ID issuance (eg, 1 per second or less).


Usual answer to many questions on non-relational DBs: denormalize wisely, by keeping a model with the sole purpose of counting and a single entity -- in the factory function building entities of other model, transactionally increment said counter and use the value in the building. If this proves to be a bottleneck, consider sharded counters or other parallelization techniques for counters -- again, just as usual for App Engine (the fact that you're using that counter as a UID does not really affect this choice).


For Python GAE SDK, you can increase the argument "limit" of the count method: https://developers.google.com/appengine/docs/python/datastore/queryclass#Query_count

Replicated from Google AppEngine: how to count a database's entries beyond 1000?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜