GAE 查询计数器 (+1000)

发布于 2024-09-17 00:00:56 字数 648 浏览 4 评论 0原文

如何将此查询计数器实现到现有类中?主要目的是我有超过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

活泼老夫 2024-09-24 00:00:56

要查找所有成员,您可以像这样使用它:

num_members = query_counter(Members.all())

但是,您可能会发现它运行缓慢,因为它进行了大量数据存储调用。

更快的方法是拥有一个单独的模型类(例如 MembersCount),并在其中维护计数(即创建成员时加 1,删除成员时减 1)。

如果您经常创建和删除成员,则可能需要创建分片计数器才能获得良好的性能 - 请参阅此处了解详细信息:

http://code.google.com/appengine/articles/sharding_counters.html

要计算某个群组中的成员数量,您可以执行以下操作:

group = ...
num_members = query_counter(Members.all().filter('group =', group.key()))

如果您预计有大量成员组中的成员数量,您还可以通过使用由组分片的计数器模型来更有效地做到这一点。

To find all members you would use it like this:

num_members = query_counter(Members.all())

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:

group = ...
num_members = query_counter(Members.all().filter('group =', group.key()))

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文