在 MongoDB 副本集集群中建立索引

Indexing in MongoDB Replica Set Cluster

在我的 replica set mongodb cluster 中,可能会出现 read 查询:

- either on column1
- or on column2
- or on combination of column1 and column2
- or on combination of column1 and column3
- or on combination of column2 and column3
- or on combination of column1 and column2 and column3

为上述每种情况创建 6 个索引是否有意义,或者有一种有效的方法可以将这些索引合并到较少数量的索引中?

{column1: 1, column2: 1, column 3:1 } 上的索引可以为以下查询提供服务:

  • 第 1 列
  • 第 1 列和第 2 列
  • 第 1 列和第 2 列以及第 3 列

{column2: 1, column3: 1} 上的索引可以为以下查询提供服务:

  • 第 2 列
  • 第 2 列和第 3 列

{column3: 1, column1: 1} 上的索引可以为以下查询提供服务:

  • 第 3 列
  • 第 3 列和第 1 列

您注意到的所有查询都可以使用 3 个复合索引妥善处理。

如果您确实发现有必要创建大量索引,请注意 mongod 对每个集合有 64 个索引的限制。

创建三个索引,每个字段一个。 Mongo 可以合并多个索引,参见 Index Intersection

验证性能并查看执行计划(使用 explain()