Google AppEngine:如何计算超过1000的数据库条目?

时间:2022-11-22 23:35:18

Duplicate of "how does one get a count of rows in a datastore model in google appengine?"

重复一下“如何在google appengine中获取数据存储模型中的行数?”


I want to know how many users I have. Previously, I achieved this with the following code:

我想知道我有多少用户。以前,我使用以下代码实现了此目的:

users = UserStore.all()
user_count = users.count()

But now I have more than 1,000 users and this method continues to return 1,000.

但现在我有超过1,000个用户,这种方法继续返回1,000。

Is there an efficient programmatic way of knowing how many users I have?

是否有一种有效的编程方式来了解我拥有多少用户?

5 个解决方案

#1


It is indeed a duplicate and the other post describes how to theoretically do it, but I'd like to stress that you should really not be doing counts this way. The reason being that BigTable by its distributed nature is really bad for aggregates. What you probably want to do is add a transactional counter to that entity, and if there are lots of transactions a sharded counter. See: http://code.google.com/appengine/articles/sharding_counters.html

它确实是一个复制品而另一篇文章描述了理论上如何做到这一点,但我想强调一下,你真的不应该这样做。原因是BigTable的分布式特性对聚合来说真的很糟糕。您可能想要做的是向该实体添加一个事务计数器,如果有很多事务,则使用分片计数器。请参阅:http://code.google.com/appengine/articles/sharding_counters.html

UPDATE: Since 1.3.1 cursors make stuff like this a lot easier: http://code.google.com/appengine/docs/python/datastore/queriesandindexes.html#Query_Cursors

更新:由于1.3.1游标使这样的东西变得更容易:http://code.google.com/appengine/docs/python/datastore/queriesandindexes.html#Query_Cursors

#2


Use pagination like these examples here.

像这些例子一样使用分页。

#3


Since version 1.3.6 of the SDK the limit of 1000 on the count function has been removed. So a call to the count function will now return the exact number of entities, even if there are more than 1000. Only limitation would be if you had so many entities that the count function would not return before the request has a timeout.

从SDK的1.3.6版开始,计数功能的限制为1000已被删除。因此,对count函数的调用现在将返回实体的确切数量,即使有超过1000个实数。只有在你有这么多实体的情况下才会有限制,即在请求超时之前count函数不会返回。

#4


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

对于Python GAE SDK,您可以增加计数方法的参数“limit”:https://developers.google.com/appengine/docs/python/datastore/queryclass#Query_count

#5


I have write this method to count a query, but how said Nick Johnson maybe it's a bad idea...

我已经写了这个方法来计算一个查询,但尼克约翰逊怎么说这可能是一个坏主意......

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

#1


It is indeed a duplicate and the other post describes how to theoretically do it, but I'd like to stress that you should really not be doing counts this way. The reason being that BigTable by its distributed nature is really bad for aggregates. What you probably want to do is add a transactional counter to that entity, and if there are lots of transactions a sharded counter. See: http://code.google.com/appengine/articles/sharding_counters.html

它确实是一个复制品而另一篇文章描述了理论上如何做到这一点,但我想强调一下,你真的不应该这样做。原因是BigTable的分布式特性对聚合来说真的很糟糕。您可能想要做的是向该实体添加一个事务计数器,如果有很多事务,则使用分片计数器。请参阅:http://code.google.com/appengine/articles/sharding_counters.html

UPDATE: Since 1.3.1 cursors make stuff like this a lot easier: http://code.google.com/appengine/docs/python/datastore/queriesandindexes.html#Query_Cursors

更新:由于1.3.1游标使这样的东西变得更容易:http://code.google.com/appengine/docs/python/datastore/queriesandindexes.html#Query_Cursors

#2


Use pagination like these examples here.

像这些例子一样使用分页。

#3


Since version 1.3.6 of the SDK the limit of 1000 on the count function has been removed. So a call to the count function will now return the exact number of entities, even if there are more than 1000. Only limitation would be if you had so many entities that the count function would not return before the request has a timeout.

从SDK的1.3.6版开始,计数功能的限制为1000已被删除。因此,对count函数的调用现在将返回实体的确切数量,即使有超过1000个实数。只有在你有这么多实体的情况下才会有限制,即在请求超时之前count函数不会返回。

#4


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

对于Python GAE SDK,您可以增加计数方法的参数“limit”:https://developers.google.com/appengine/docs/python/datastore/queryclass#Query_count

#5


I have write this method to count a query, but how said Nick Johnson maybe it's a bad idea...

我已经写了这个方法来计算一个查询,但尼克约翰逊怎么说这可能是一个坏主意......

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