如何使Firestore索引合并生效? [英] How to get Firestore Index Merge to work?

查看:51
本文介绍了如何使Firestore索引合并生效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Firestore索引合并以减少所需索引的数量时遇到麻烦.

请考虑以下示例情况:

Firestore集合:test/somedoc

  {答:"1",b:"1",c:"1",d:"1"} 

这将导致Firestore在测试中为a到d字段创建4个自动单字段索引.

使用一些相等条件和一种不相关的排序查询该表:

 等待db.collection('test').where('a','==','1').where('b','==','1').where('c','==','1').orderBy('d').得到(); 

这会导致Firebase失败,并提示创建包含所有字段的复合索引.这是预期的.

现在根据文档(

但是,如果您在收集测试中创建索引,例如使用c asc,d asc,查询仍然会失败,并提示创建完整的复合索引.

  UnhandledPromiseRejectionWarning:FirebaseError:查询需要索引. 

我在做什么错了?

解决方案

好,终于在这里理解了这个问题.

要使索引合并生效,您需要 composite-indices (涵盖所有相关字段的复合索引).自动生成的单个字段索引不会用于合并.

因此,如果您创建两个附加的复合索引

  • a asc,d asc
  • b asc,d asc

然后将覆盖所有字段,并且将自动使用索引合并.

I am having trouble using firestore index merging in order to reduce the number of required indices.

Consider this example situation:

Firestore Collection: test/somedoc

{
  a: '1',
  b: '1',
  c: '1',
  d: '1'
}

This will cause Firestore to create 4 automatic single field indices on test for fields a to d.

Querying this table with a few equality conditions and one unrelated sort:

    await db.collection('test')
        .where('a', '==', '1')
        .where('b', '==', '1')
        .where('c', '==', '1')
        .orderBy('d')
        .get();

This causes Firebase to fail with a prompt for a composite index creation with all fields included. This is expected.

Now according to the docs (https://firebase.google.com/docs/firestore/query-data/index-overview#taking_advantage_of_index_merging), if you already have a composite on e.g. fields c and d, firestore will use index merging instead of requiring a composite index on all fields.

However if you create an index on collection test e.g. with c asc, d asc, the query will still fail with prompting to create the full composite index.

UnhandledPromiseRejectionWarning: FirebaseError: The query requires an index. 

What am I doing wrong here?

解决方案

Ok finally understood the problem here.

In order to index merge to work, you need composite-indices that cover all related fields. The auto-generated single field indices won't be used for merging.

Consequently, if you create two additional composite indices

  • a asc, d asc
  • b asc, d asc

then all fields will be covered and index merge will be used automatically.

这篇关于如何使Firestore索引合并生效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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