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

Olie picture Olie · Feb 12, 2011 · Viewed 20.7k times · Source

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!

Answer

RodneyReid picture RodneyReid · Sep 4, 2015

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.