How to increase the performance of gae query?
<p>I have implemented a logic of querying  a table and for each entity in that particular table, I have to lookup another table.</p>

<p>For, ex.</p>

<p>My code looks like,</p>

<pre><code>query = ndb.gql("select * from Foo where user = :1", user.key)
stories, next_cursor, more = query.fetch_page(size, start_cursor=cursor)
if next_cursor:
   for story in stories:
       print story.key
       images = ndb.gql("select * from Images where story = :1", story.key)
       for image in images:
           print image.key
else:
   #do some operations
</code></pre>

<p>You see, if we give the size as 10 to the fetch_page function, it would find 10 entities each. And for each entity, we have to lookup another kind <code>Image</code>. </p>

<p>This type of datastore lookup takes 850 to 950 ms. I want to decrease the response time of this API. 
Note that I have to get some column values from <code>Story</code> kind and also from <code>Images</code> kind.</p>

<p>Is there anyway to shorten the query by using <code>get_multi</code> method. Or, I have an idea of using <code>memcache</code> or shall we define a new <code>StructuredProperty</code> in the <code>Foo</code> model where it's value must be a list of <code>Images</code> model entities.</p>

<p>I donno which one suits in this case.. Pls guide me.</p>

<p>You will want to look at NDB batch async API</p>

<pre><code>   @ndb.tasklet
   def get_stories(user_key):
     stories = yield Story.query(Story.user_key == user_key).fetch_async()
     futs = [
       item.key.get_async() for item in stories]
     result = yield futs
     raise ndb.Return(result)

   get_stories(user_key).get_result()
</code></pre>

<p>This API will only call 2 queries.</p>

<ol>
<li>make a query to DataStore</li>
<li>with N result from the above query, make 1 query to get all stories </li>
</ol>

<p>Since, <code>Key.get_async()</code> also use memcache automatically, from the seconds time you call above function, the 2 query will call to memcache</p>

python performance google-app-engine google-cloud-datastore app-engine-ndb