Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9248353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:55:37+00:00 2026-06-18T09:55:37+00:00

For 100k+ entities in google datastore, ndb.query().count() is going to cancelled by deadline ,

  • 0

For 100k+ entities in google datastore, ndb.query().count() is going to cancelled by deadline , even with index. I’ve tried with produce_cursors options but only iter() or fetch_page() will returns cursor but count() doesn’t.

How can I count large entities?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:55:38+00:00Added an answer on June 18, 2026 at 9:55 am

    To do something that expensive you should take a look on Task Queue Python API. Based on the Task Queue API, Google App Engine provides the deferred library, which we can use to simplify the whole process of running background tasks.

    Here is an example of how you could use the deferred library in your app:

    import logging
    
    def count_large_query(query):
      total = query.count()
      logging.info('Total entities: %d' % total)
    

    Then you can call the above function from within your app like:

    from google.appengine.ext import deferred
    
    # Somewhere in your request:
    deferred.defer(count_large_query, ndb.query())
    

    While I’m still not sure if the count() going to return any results with such large datastore you could use this count_large_query() function instead, which is using cursors (untested):

    LIMIT = 1024
    def count_large_query(query):
      cursor = None
      more = True
      total = 0
      while more:
        ndbs, cursor, more = query.fetch_page(LIMIT, start_cursor=cursor, keys_only=True)
        total += len(ndbs)
    
      logging.info('Total entitites: %d' % total)
    

    To try locally the above set the LIMIT to 4 and check if in your console you can see the Total entitites: ## line.


    As Guido mentioned in the comment this will not going to scale either:

    This still doesn’t scale (though it may postpone the problem). A task
    has a 10 minute instead of 1 minute, so maybe you can count 10x as
    many entities. But it’s pretty expensive! Have a search for sharded
    counters if you want to solve this properly (unfortunately it’s a lot
    of work).

    So you might want to take a look on best practices for writing scalable applications and especially the sharding counters.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

WITH TOP 100000 (100k) this query is finished in about 3 seconds WITH TOP
Total row-count is in the range 10k-100k rows. Can I use RAND() on force.com?
I am using Google Datastore for my application and now for reporting purposes i
I need to collect some statistics on my entities in the datastore. As an
i have 100k indexed pages in google http://www.domain.com/lol-1 http://www.domain.com/lol-2 http://www.domain.com/lol-3 ... I wanted to
I have about 100k Outlook mail items that have about 500-600 chars per Body.
I have a large (more than 100K objects) collection of Java objects like below.
which is more efficient (when managing over 100K records): A. Mysql SELECT * FROM
I have a list of say 100k floats and I want to convert it
I have a table with about 100k records and I want to delete some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.