You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
is there a way to get the count/length of the entities that matched a query without actually receiving the entities data? or can something like that be achieved via BigQuery?
Nope, Datastore has no way to count. Even in the SDKs which offer a count method, these are all O(n).
Here be dragons and all that:
I would not recommend implementing this, but the "fastest" way to do this would be to run a your query with projection = '__key__' with offset = 1000 and limit = 0. Then keep running this counting the number of skipped_results and using the skipped_cursor to get the next batch.
This way, Datastore doesn't return any results, just the counts. However, you still have to pay for each batch (the key projection makes it only one read per batch though). More importantly, Datastore still has to load all the results, so the time it takes (and the cost) will scale with the size of your count.
Activity
jgeewax commentedon Jun 25, 2015
I think you might do this with a keys-only query. @stephenplusplus , how do we do that in gcloud-node?
abelino commentedon Jun 25, 2015
looking at GQL I think it would be the following:
SELECT __key__ FROM myKindgoing to look at dataset class right now.but what if the keys being returned is a ridiculous amount?
stephenplusplus commentedon Jun 25, 2015
You can run a keys-only query by using
select('__key__')when building your query. Here's a start to finish example:abelino commentedon Jun 25, 2015
will the following construct work:
will test later on, and come back if I have any questions. Thanks for the help guys @stephenplusplus @jgeewax
dhermes commentedon Jun 25, 2015
This will still return all the keys, which is O(n) in the payload size, when you really just want one integer, which will be O(1).
jgeewax commentedon Jun 25, 2015
Is there a way to ask for COUNT(key)?
abelino commentedon Jun 25, 2015
@dhermes Exactly, that is the ideal solution, but not sure if COUNT func is supported by GQL.
stephenplusplus commentedon Jun 25, 2015
I couldn't find anything in the docs about just getting the resulting length of a query. @pcostell might know if this is possible?
pcostell commentedon Jun 25, 2015
Nope, Datastore has no way to count. Even in the SDKs which offer a
countmethod, these are all O(n).Here be dragons and all that:
I would not recommend implementing this, but the "fastest" way to do this would be to run a your query with
projection = '__key__'withoffset = 1000andlimit = 0. Then keep running this counting the number ofskipped_resultsand using theskipped_cursorto get the next batch.This way, Datastore doesn't return any results, just the counts. However, you still have to pay for each batch (the key projection makes it only one read per batch though). More importantly, Datastore still has to load all the results, so the time it takes (and the cost) will scale with the size of your count.
stephenplusplus commentedon Jun 25, 2015
@abelino sorry to skip your earlier question. Your example filtering the same query should work just fine.
And thanks as always, @pcostell!
@jgeewax would it be a good idea to add an example of a keys-only query to the docs?
jgeewax commentedon Jun 29, 2015
Yea, I think mentioning that somewhere (ever so briefly) would be a good idea...
6 remaining items
chore(deps): update dependency sinon to v8 (#690)
build: track flaky tests for "nightly", add new secrets for tagging (#…
build: track flaky tests for "nightly", add new secrets for tagging (#…
build: track flaky tests for "nightly", add new secrets for tagging (#…
build: track flaky tests for "nightly", add new secrets for tagging (#…
build: track flaky tests for "nightly", add new secrets for tagging (#…
chore(deps): update dependency uuid to v8 (#690)
migrate code from googleapis/cloud-profiler-nodejs (#7087)
docs: fix return type in docstring for update method (#690)
feat: use JSON proto for transaction-runner (#690)
chore: release 3.7.2 (#690)
fix: make sure generated protos.js have unique root name (#690)
fix: make sure generated protos.js have unique root name (#690)
fix: make sure generated protos.js have unique root name (#690)
fix: make sure generated protos.js have unique root name (googleapis#690