问题描述
在我的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 }
上的索引可以为以下查询提供服务:
- column1
- 第1列和第2列
- 第1列,第2列和第3列
{column2: 1,column3: 1}
上的索引可以为以下查询提供服务:
- column2
- 第2列和第3列
{column3: 1,column1: 1}
上的索引可以为以下查询服务:
- column3
- column3和column1
所有提到的查询都可以使用3个复合索引正确处理。
如果确实需要创建大量索引,请注意mongod每个集合最多只能有64个索引。
,创建三个索引,每个字段一个。 Mongo可以合并多个索引,请参见Index Intersection
验证性能并查看执行计划(使用explain()
)