如果没有组合索引,多属性ndb查询如何成功? [英] How can a multi-property ndb query be successful without a composite index?

查看:86
本文介绍了如果没有组合索引,多属性ndb查询如何成功?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个实体模型:

  class ApartCILabel(ndb.Model):
project_id = ndb.IntegerProperty (indexed = True)
changeset_ids = ndb.IntegerProperty(repeated = True,indexed = True)#ApartCIChangeset IDs
#其他属性

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

pre $ keys = ApartClLabel.query(ApartCILabel.project_id == self.db_data.project_id,
ApartCILabel.changeset_ids == self.key_id).fetch(keys_only = True)
如果键:
label = Label( db_key = keys [0],handler = self.handler)
logging.debug('找到标签:%s'%label.name)
else:
logging.error('label for %s找不到'%self.lid)

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

  DEBUG 2018-02-20 23:56: 11,720 ci_changeset.py:70]找到的标签:ACI_COPY_OF_SMALL_180221_1 

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



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



我检查了我的 index.yaml 文件,这种唯一的组合索引是这样的:

   -  kind: ApartClLabel 
属性:
- 名称:project_id
- 名称:创建
方向:desc

我在开发者控制台中检查了数据存储区索引,正如我所料,它与 index.yaml 文件同步:





我甚至在开发者控制台,它也应该需要一个复合索引。同样成功:



这怎么可能这样的查询功能没有复合索引?我缺少什么?



更新:



几小时后,我执行了另一个类似的手动查询,结果是正确的,但这次您的数据存储没有这个查询所需的组合索引(开发人员提供)。错误(或者我应该把它叫做警告?)出现:



解决方案

div>

仅使用相等过滤器的查询不需要创建组合索引。从文档中:


Cloud Datastore为以下表单的查询提供了内置或自动索引:


  • 仅使用祖先和平等过滤器的查询


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

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:

解决方案

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

这篇关于如果没有组合索引,多属性ndb查询如何成功?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆