GAE 查询计数器 (+1000)
如何将此查询计数器实现到现有类中?主要目的是我有超过 3000 条记录的成员数据存储模型。我只想计算它的总记录,并在应用程序引擎食谱中找到了这个:
def query_counter (q, cursor=None, limit=500):
if cursor:
q.with_cursor (cursor)
count = q.count (limit=limit)
if count == limit:
return count + query_counter (q, q.cursor (), limit=limit)
return count
我现有的模型是:
class Members(search.SearchableModel):
group = db.ListProperty(db.Key,default=[])
email = db.EmailProperty()
name = db.TextProperty()
gender = db.StringProperty()
此外,我想计算通过列表引用加入特定组的成员。它还可能包含超过 1000 条记录。
有人有用于此目的的 query.cursor 的经验吗?
How to implement this query counter into existing class? The main purpose is i have datastore model of members with more than 3000 records. I just want to count its total record, and found this on app engine cookbook:
def query_counter (q, cursor=None, limit=500):
if cursor:
q.with_cursor (cursor)
count = q.count (limit=limit)
if count == limit:
return count + query_counter (q, q.cursor (), limit=limit)
return count
My existing model is:
class Members(search.SearchableModel):
group = db.ListProperty(db.Key,default=[])
email = db.EmailProperty()
name = db.TextProperty()
gender = db.StringProperty()
Further i want to count members that join certain group with list reference. It might content more than 1000 records also.
Anyone have experience with query.cursor for this purpose?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要查找所有成员,您可以像这样使用它:
但是,您可能会发现它运行缓慢,因为它进行了大量数据存储调用。
更快的方法是拥有一个单独的模型类(例如 MembersCount),并在其中维护计数(即创建成员时加 1,删除成员时减 1)。
如果您经常创建和删除成员,则可能需要创建分片计数器才能获得良好的性能 - 请参阅此处了解详细信息:
http://code.google.com/appengine/articles/sharding_counters.html
要计算某个群组中的成员数量,您可以执行以下操作:
如果您预计有大量成员组中的成员数量,您还可以通过使用由组分片的计数器模型来更有效地做到这一点。
To find all members you would use it like this:
However, you may find that this runs slowly, because it's making a lot of datastore calls.
A faster way would be to have a separate model class (eg MembersCount), and maintain the count in there (ie add 1 when you create a member, subtract 1 when you delete a member).
If you are creating and deleting members frequently you may need to create a sharded counter in order to get good performance - see here for details:
http://code.google.com/appengine/articles/sharding_counters.html
To count members in a certain group, you could do something like this:
If you expect to have large numbers of members in a group, you could also do that more efficiently by using a counter model which is sharded by the group.