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>I don't know whole structure of your project, but...</p>

<p>You can do something like that:</p>

<pre><code>class Story(ndb.Model):
    images = ndb.KeyProperty(kind=Image, repeated=True)
    user = ndb.KeyProperty(kind=User)
</code></pre>

<p>and every time user will add new image update it (<code>images</code> property of <code>Story</code>).</p>

<p>Then you'll be able to use:</p>

<pre><code>images = []
stories = Story.query.filter(Story.user == user.key)
stories = stories.fetch(size)
for story in stories:
    images.extend(ndb.get_multi(story.images))
print images
</code></pre>

<p>Hope that helps.</p>

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