What's the best way to count results in GQL?

后端 未结 9 1533
情深已故
情深已故 2020-12-02 14:21

I figure one way to do a count is like this:

foo = db.GqlQuery(\"SELECT * FROM bar WHERE baz = \'baz\')
my_count = foo.count()

What I don\'

9条回答
  •  独厮守ぢ
    2020-12-02 15:01

    According to the GqlQuery.count() documentation, you can set the limit to be some number greater than 1000:

    from models import Troll
    troll_count = Troll.all(keys_only=True).count(limit=31337)
    

    Sharded counters are the right way to keep track of numbers like this, as folks have said, but if you figure this out late in the game (like me) then you'll need to initialize the counters from an actual count of objects. But this is a great way to burn through your free quota of Datastore Small Operations (50,000 I think). Every time you run the code, it will use up as many ops as there are model objects.

提交回复
热议问题