App Engine Datastore Viewer,如何使用 GQL 显示记录数?

发布于 2024-10-17 06:25:42 字数 303 浏览 7 评论 0 原文

我认为这对于类似 SQL 的人来说很容易!我想要的是 GQL 相当于:

select count(*) from foo;

并得到类似于以下内容的答案:

1972 records.

我想从基于 Web 的 DataStore 查看器中的“命令行”在 GQL 中执行此操作。 (你知道,那个一次显示 20 个并让我看到“下一个 20”)

无论如何 - 我确信这很容易,我只是似乎无法找到正确的语法。任何帮助将不胜感激。

谢谢!

I would think this would be easy for an SQL-alike! What I want is the GQL equivalent of:

select count(*) from foo;

and to get back an answer something similar to:

1972 records.

And I want to do this in GQL from the "command line" in the web-based DataStore viewer. (You know, the one that shows 20 at a time and lets me see "next 20")

Anyway -- I'm sure it's brain-dead easy, I just can't seem to find the correct syntax. Any help would be appreciated.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

挽容 2024-10-24 06:25:42

对于直接的数据存储控制台,没有直接的方法来做到这一点,但我只是想出了如何使用 OFFSET 关键字间接做到这一点。

因此,给定一个表,我们将调用 foo,其中有一个名为 type 的字段,我们要检查名为“bar”的值:(

SELECT * FROM foo WHERE type="bar" OFFSET 1024

我们将在这里做一个“更暖、更冷”的快速游戏,二进制风格)

让我们假设该查询没有返回任何内容。将 OFFSET 更改为 512,然后是 256、128、64……您就明白了。反过来也是一样:转到 2048、4096、8192、16384 等,直到看不到任何记录,然后退出。

我刚刚在工作中做了一个。从 2048 开始,注意到出现了两条记录。表中有 2049 个。在更极端的情况下,(假设有 3300 条记录),您可以从 2048 开始,注意有很多,转到 4096,没有……取中点(2048 和 4096 之间的 1024 是 3072),然后注意您有记录...从那里你可以加上之前中点的一半 (512) 得到 3584,但没有。削减一半(256)得到 3328,仍然没有。再次下降一半 (128) 达到 3200,并创造了记录。上升到最后一个值的一半(64),仍然有记录。再上升一半 (32) 到 3296 - 仍然是记录,但很小,您可以轻松看到正好有 3300 条。

与数据存储统计信息相比,查看表中有多少条记录的好处是您可以通过 WHERE 来限制它条款。

With straight Datastore Console, there is no direct way to do it, but I just figured out how to do it indirectly, with the OFFSET keyword.

So, given a table, we'll call foo, with a field called type that we want to check for values named "bar":

SELECT * FROM foo WHERE type="bar" OFFSET 1024

(We'll be doing a quick game of "warmer, colder" here, binary style)

Let's say that query returns nothing. Change OFFSET to 512, then 256, 128, 64, ... you get the idea. Same thing in reverse: Go up to 2048, 4096, 8192, 16384, etc. until you see no records, then back off.

I just did one here at work. Started with 2048, and noticed two records came up. There's 2049 in the table. In a more extreme case, (lets say there's 3300 records), you could start with 2048, notice there's a lot, go to 4096, there's none... Take the midpoint (1024 between 2048 and 4096 is 3072) next and notice you have records... From there you could add half the previous midpoint (512) to get 3584, and there's none. Whittle back down half (256) to get 3328, still none. Once more down half (128) to get 3200 and there's records. Go up half of the last val (64) and there's still records. Go up half again (32) to 3296 - still records, but so small you can easily see there's exactly 3300.

The nice thing about this vs. Datastore statistics to see how many records are in a table is you can limit it by the WHERE clause.

风追烟花雨 2024-10-24 06:25:42

我认为没有任何直接的方法可以通过 GQL 获取实体的数量。但是您可以直接从仪表板获取计数
;

输入图像描述此处

更多详细信息 - https://cloud.google .com/appengine/docs/python/console/managing-datastore

I don't think there is any direct way to get the count of entities via GQL. However you can get the count directly from the dashbaord
;

enter image description here

More details - https://cloud.google.com/appengine/docs/python/console/managing-datastore

帅气称霸 2024-10-24 06:25:42

正如 其他 问题,看起来没有 count 聚合GQL 中的函数。 GQL 参考也没有说有这种能力这样做,尽管它没有明确表示这是不可能的。

在开发控制台(本地运行应用程序)中,看起来只需单击“列出实体”按钮就会显示某种类型的所有实体的列表,并且您可以看到“结果 1-10(某些数字)”获取您的开发环境中的总数。

在生产中,您可以使用“数据存储统计信息”选项卡(数据存储查看器正下方的链接),选择“显示统计信息:(您的实体类型)”,它将显示实体总数,但这不是最新的数据视图(“每天至少更新一次”)。

由于您无法通过浏览器在生产中运行任意代码,因此我认为“在查询上使用 .count()”不会有帮助,但如果您使用 远程 API.count() 方法是 自 2010 年 8 月起,条目数量不再限制在 1000 个,因此您应该能够运行 print MyEntity.all().count() 并获得您想要的结果。

As it's stated in other questions, it looks like there is no count aggregate function in GQL. The GQL Reference also doesn't say there is the ability to do this, though it doesn't explicitly say that it's not possible.

In the development console (running your application locally) it looks like just clicking the "List Entities" button will show you a list of all entities of a certain type, and you can see "Results 1-10 of (some number)" to get a total count in your development environment.

In production you can use the "Datastore Statistics" tab (the link right underneath the Datastore Viewer), choose "Display Statistics for: (your entity type)" and it will show you the total number of entities, however this is not the freshest view of the data (updated "at least once per day").

Since you can't run arbitrary code in production via the browser, I don't think saying "use .count() on a query" would help, but if you're using the Remote API, the .count() method is no longer capped at 1000 entries as of August, 2010, so you should be able to run print MyEntity.all().count() and get the result you want.

庆幸我还是我 2024-10-24 06:25:42

这是数据存储无法做到的令人惊讶的事情之一。我认为最快的方法是从 foo 中选择 __KEY__ 到列表中,然后对列表中的项目进行计数(在基于网络的查看器中无法执行此操作)。

如果您对可能有点过时的统计信息感到满意,您可以转到管理控制台的“数据存储统计信息”页面,该页面将告诉您一段时间之前每种类型有多少个实体。这些统计数据似乎通常不到 10 小时。不幸的是,您无法更具体地查询它们。

This is one of those surprising things that the datastore just can't do. I think the fastest way to do it would be to select __KEY__ from foo into a List, and then count the items in the list (which you can't do in the web-based viewer).

If you're happy with statistics that can be a little bit stale, you can go to the Datastore Statistics page of the admin console, which will tell you how many entities of each type there were some time ago. It seems like those stats are usually less than 10 hours old. Unfortunately, you can't query them more specifically.

混浊又暗下来 2024-10-24 06:25:42

无法获取 GQL 中的总数。以下是使用 python 获取计数的方法:

def count_models(model_class, max_fetch=1000):
  total = 0
  cursor = None
  while True:
    query = model_class.all(keys_only=True)
    if cursor: 
        query.with_cursor(cursor)
    results = query.fetch(max_fetch)
    total += len(results)
    print('still counting: ' + total)
    if (len(results) < max_fetch):
        return total
    cursor = query.cursor()

您可以使用 remote_api_shell 运行此函数,或者添加 自定义页面到您的管理站点以运​​行此查询。显然,如果您有数百万行,您将需要等待一段时间。您也许可以增加 max_fetch,我不确定当前的提取限制是多少。

There's no way to get a total count in GQL. Here's a way to get a count using python:

def count_models(model_class, max_fetch=1000):
  total = 0
  cursor = None
  while True:
    query = model_class.all(keys_only=True)
    if cursor: 
        query.with_cursor(cursor)
    results = query.fetch(max_fetch)
    total += len(results)
    print('still counting: ' + total)
    if (len(results) < max_fetch):
        return total
    cursor = query.cursor()

You could run this function using the remote_api_shell, or add a custom page to your admin site to run this query. Obviously, if you've got millions of rows you're going to be waiting a while. You might be able to increase max_fetch, I'm not sure what the current fetch limit is.

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