如何获取数据存储区中表格中的行数? [英] How to get the number of rows in a table in a Datastore?
问题描述
在许多情况下,使用Google Application Engine了解数据存储区中表(一种)中的行数可能很有用。
没有清晰,快速的解决方案。至少我没有找到一个..你有吗?
In many cases, it could be useful to know the number of rows in a table (a kind) in a datastore using Google Application Engine.
There is not clear and fast solution . At least I have not found one.. Have you?
推荐答案
你可以有效地获得所有实体(即表中的行数),使用数据存储统计信息。简单示例:
You can efficiently get a count of all entities of a particular kind (i.e., number of rows in a table) using the Datastore Statistics. Simple example:
from google.appengine.ext.db import stats
kind_stats = stats.KindStat().all().filter("kind_name =", "NameOfYourModel").get()
count = kind_stats.count
b $ b
您可以找到更详细的示例,了解如何获取最新的统计信息此处(GAE可能会保留多个统计信息 - 一个在5分钟前,一个在30分钟前等)。
You can find a more detailed example of how to get the latest stats here (GAE may keep multiple copies of the stats - one for 5min ago, one for 30min ago, etc.).
请注意,这些统计数据并不是不断更新,因此它们滞后于实际计数。如果你真的需要实际的计数,那么你可以跟踪自己的自定义stats表中的计数,并更新它,每次创建/删除一个实体(虽然这将是一个更昂贵的事情)。
Note that these statistics aren't constantly updated so they lag a little behind the actual counts. If you really need the actual count, then you could track counts in your own custom stats table and update it every time you create/delete an entity (though this will be quite a bit more expensive to do).
更新03-08-2015 :使用数据存储统计信息可能导致过时的结果。如果这不是一个选项,另外两个方法是保持计数器或分片计数器。 (您可以在这里阅读更多内容)。如果您需要实时结果,只需查看这2个。
Update 03-08-2015: Using the Datastore Statistics can lead to stale results. If that's not an option, another two methods are keeping a counter or sharding counters. (You can read more about those here). Only look at these 2 if you need real-time results.
这篇关于如何获取数据存储区中表格中的行数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!