如何获取数据存储中表中的行数? [英] How to get the number of rows in a table in a Datastore?

查看:29
本文介绍了如何获取数据存储中表中的行数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在许多情况下,使用 Google 应用程序引擎了解数据存储区中表(一种)中的行数可能很有用.
没有明确快速的解决方案.至少我没有找到..你找到了吗?

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

您可以找到有关如何获取最新统计数据的更详细示例此处(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.).

请注意,这些统计数据不会不断更新,因此它们会稍微落后于实际计数.如果您真的需要实际计数,那么您可以在您自己的自定义统计表中跟踪计数,并在每次创建/删除实体时更新它(尽管这样做的成本会更高).

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).

2015 年 3 月 8 日更新:使用 Datastore 统计信息可能会导致结果过时.如果这不是一个选项,另外两种方法是保留一个计数器或分片计数器.(您可以在此处阅读有关这些内容的更多信息).如果您需要实时结果,请仅查看这 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆