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 can add a property to each Story that contains a list of Image ids. I assume that this list rarely changes. Then you can easily <code>get_multi</code> all images related to a story without any queries.</p>

<p>You may also consider to <code>get_multi</code> all images for all stories, returned by your query, in a single call, and then "attach" them to respective stories in your code, if necessary.</p>

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