title: How to avoid datastore_v3.Next calls
body: <p>My datastore processing takes lot of times.
  So, I checked what process takes time with StackDriver -> Trace.
  Then, "datastore_v3.Next" is called many times.</p>
  
  <p>I found following document.
  <a href="https://cloud.google.com/appengine/docs/standard/python/tools/appstats" rel="nofollow noreferrer">https://cloud.google.com/appengine/docs/standard/python/tools/appstats</a></p>
  
  <blockquote>
    <p>Datastore queries usually involve a datastore_v3.RunQuery followed by
    zero or more datastore_v3.Next calls. (RunQuery returns the first few
    results, so the API only uses Next when fetching many results.
    Avoiding unnecessary Next calls may speed up your app!)</p>
  </blockquote>
  
  <p>But, I don't understand</p>
  
  <ul>
  <li>In what cases will datastore_v3.Next be called many time?</li>
  <li>How to avoid Next calls?</li>
  </ul>
  
  <p>Add:
  MyCode is below</p>
  
  <pre><code>    @classmethod
      def get_foo(cls, user_key, foo):
        search_key = ndb.Key('UserInformation', user_key.id(), 'Foo', foo)
        return search_key.get()
  </code></pre>
  
  <p>UserInformation and Foo kind have 1 million entity.</p>