多属性ndb查询没有复合索引怎么能成功呢?

How can a multi-property ndb query be successful without a composite index?

我有这个实体模型:

class ApartCILabel(ndb.Model):
    project_id = ndb.IntegerProperty(indexed=True)
    changeset_ids = ndb.IntegerProperty(repeated=True, indexed=True)   # ApartCIChangeset IDs
    # other properties

我最近为这些实体添加了一种新的查询类型:

            keys = ApartCILabel.query(ApartCILabel.project_id == self.db_data.project_id,
                                      ApartCILabel.changeset_ids == self.key_id).fetch(keys_only=True)
            if keys:
                label = Label(db_key=keys[0], handler=self.handler)
                logging.debug('label found: %s' % label.name)
            else:
                logging.error('label for %s not found' % self.lid)

我知道我需要一个复合索引,所以我 运行 dev_appserver.py 中的应用程序用于自动更新我的 index.yaml 文件。在日志中看到调试信息,说明查询成功:

DEBUG    2018-02-20 23:56:11,720 ci_changeset.py:70] label found: ACI_COPY_OF_SMALL_180221_1

令我惊讶的是,我在 index.yaml 文件中没有看到任何更新。好的,自从上次我需要索引更新(现在 运行 1.9.65)以来,我确实更新了我的 GAE SDK,也许我正在使用基于符号链接的方案在我所有的共享我的索引定义文件服务以某种方式干扰。没问题,我将在 GAE 上部署更改,我会在那里看到缺少复合索引错误。

再次惊喜:查询成功,没有报错

我检查了我的 index.yaml 文件,这种类型的唯一复合索引是这个:

- kind: ApartCILabel
  properties:
  - name: project_id
  - name: created
    direction: desc

我仔细检查了开发人员控制台中的数据存储索引,如我所料,它与 index.yaml 文件同步:

我在开发者控制台手动查询过,应该也是需要复合索引的。同样成功:

如果没有复合索引,这样的查询怎么可能运行?我错过了什么?

更新:

几个小时后,我执行了另一个类似的手动查询,结果再次正确,但是这次 Your Datastore does not have the composite index (developer-supplied) required for this query. 错误(或者我应该称之为警告?)出现了:

仅使用相等过滤器的查询不需要创建复合索引。来自文档:

Cloud Datastore provides built-in, or automatic, indexes for queries of the following forms:

  • Queries using only ancestor and equality filters

https://cloud.google.com/datastore/docs/concepts/indexes#index_configuration