Note that there are some explanatory texts on larger screens.

plurals
  1. POIs batch fetching db.get(keys) preserving the keys order?
    text
    copied!<p>Google App Engine supports a fetch operation based on a list of keys <a href="http://code.google.com/appengine/docs/python/datastore/functions.html" rel="nofollow noreferrer">google.appengine.ext.db.get(<i>keys</i>)</a>.</p> <p>I'd be interested to figure out if there is any guarantee that the result list preserves the order of the keys (i.e. keys = [<code>k_1</code>, <code>k_2</code>, <code>k_3</code>] then for the result [<code>r_1</code>, <code>r_2</code>, <code>r_3</code>] is always true that <code>r_i</code>.key() == <code>k_i</code>).</p> <p>As far as I know, the API is performing the IN selects by internally issuing N sub-selects for each value in IN. I would expect this to happen for <code>db.keys</code> and so the call would preserve the keys order.</p> <p>Anyways, I am not sure and I cannot find any reference that <code>db.keys</code> is equivalent to an IN select though and if there aren't any optimizations for its execution in place. Otherwise, the workaround would be quite simple (I would iterate and query myself for each key and so I'll have the guarantee that the I don't depend on <code>db.keys</code> implementation).</p> <p>I have run some basic tests and the results are showing that:</p> <ol> <li><code>db.get()</code> performs best</li> <li><code>db.get()</code> preserves the keys order</li> <li>the alternative <code>Model.<code>get_by_id</code></code> (for which the order of results will always be guaranteed) is performing slower</li> </ol> <p>While the results seem to confirm my assumptions, I am wondering if others have investigated this and have reached similar or different conclusions.</p> <p>tia, ./alex</p> <p>Doing some more research I have found the following (documentation for both <code>db.get()</code> and <code>Model.get()</code>: </p> <blockquote> <p>If ids is a list, the method returns a list of model instances, with a None value when no entity exists for a corresponding Key.</p> </blockquote> <p>Even if it doesn't underline it, I think it is clear that the order is guaranteed.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload