App Engine Datastore Viewer, how to show count of records using GQL?

22,110

Solution 1

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.

Solution 2

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.

Solution 3

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

Share:
22,110
Olie
Author by

Olie

I like to code. I also make beer and, occasionally, some rock & roll.

Updated on July 19, 2022

Comments

  • Olie
    Olie almost 2 years

    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!