擅长:python、mysql、java
<p>祖先查询有一种互通有无的关系。它们处理起来更加冗长和混乱,但是您可以获得更好的数据结构和查询的一致性。你知道吗</p>
<p>为了简化这一点,如果处理程序知道要获取的BEntity,只需传递<code>key.urlsafe()</code>编码键,它已经对所有祖先信息进行了编码。你知道吗</p>
<p>如果这不可能,请尝试重新构造数据。由于这些对象都是同一祖先,因此它们属于同一实体组,因此最多每秒可以为该实体组中的对象插入/更新~1次。如果您需要更高的吞吐量或不需要一致的祖先查询,那么请尝试使用<code>ndb.KeyProperty</code>将实体链接到父级引用,而不是作为祖先。然后您只需要查询单个父级,而不需要查询父级和父级的父级。你知道吗</p>
<p>您还应该尽可能尝试使用ID,这样就可以避免按属性筛选数据存储中的实体,而只按ID引用它们:</p>
<p><code>BEntity.query(ancestor = ndb.Key("RootEntity", 1, "AEntity", int(some_value)))</code></p>
<p>这里,<code>int(some_value)</code>是创建对象时使用的<code>AEntity</code>的整数ID。只需确保手动创建/使用的ID在共享同一父级的模型的所有实例中都是唯一的。</strong></p>
<p>编辑:
为了澄清,我的最后一个例子应该更清楚,因为我建议重新构造数据,以便<code>int(some_value)</code>用作<code>AEntity</code>的整数ID,而不是将is作为实体的一个单独属性存储—当然,如果可能的话。在给定的示例中,对具有给定整型字段值<code>int(some_value)</code>的<code>AEntity</code>对象执行查询,并使用<code>get()</code>执行-这意味着您将始终期望该整型ID返回一个值,从而使其成为用作该对象键的整型ID的很好的候选对象,从而消除了查询的需要。你知道吗</p>