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

瘦欲@ 提交于 2019-11-28 05:32:37

问题


I have this entity model:

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

I recently added a new type of query for these entities:

            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)

I knew I needed a composite index for it, so I ran the app in dev_appserver.py, which used to automatically update my index.yaml file. I see the debug message in the log, indicating that the query was successful:

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

To my surprise, I see no update in my index.yaml file. OK, I did update my GAE SDK since the last time I needed an index update (running 1.9.65 now), maybe the symlink-based scheme I'm using for sharing my index definition file across all my services interferes somehow. No problem, I'm gonna deploy the change on GAE and I'll see the missing composite index error there.

Again surprise: the query is successful, no error.

I checked my index.yaml file, the only composite index for this kind is this one:

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

I double-checked the datastore indexes in the developer console, which is, as I expected, in sync with the index.yaml file:

I even performed a manual query in the developer console, which should also require a composite index. Also successful:

How is it possible for such query to function without a composite index? What am I missing?

UPDATE:

After a few hours I performed another similar manual query, again the result is correct, but this time the Your Datastore does not have the composite index (developer-supplied) required for this query. error (or should I rather call it warning?) showed up:


回答1:


Queries using only equality filters do not require a composite index to be created. From the documentation:

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



来源:https://stackoverflow.com/questions/48896357/how-can-a-multi-property-ndb-query-be-successful-without-a-composite-index

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!