mongodb化合物或个别指数

时间:2013-08-30 09:28:56

标签: mongodb performance indexing database

我需要一些关于如何正确设置索引w / mongodb的建议。

假设我的数据收集是这样的:

球员

 - age
 - scores
 - fouls
 - yellow cards
 - red cards
 - offsides

我的问题是我的潜在查询可以涵盖所有索引组合,例如:

Get players that age < 30 and yellow card > 3
Get players that age < 30 and yellow card > 3 and red_cards >6
Get players that age < 30 and red_cards card > 3 and scores > 2
Get players that scores < 30 and yellow card > 3 and fouls < 6 and red_cards >2
Get players that scores < 30 and yellow card > 3 and fouls < 6 and red_cards >2 and age > 25

在这种情况下分配索引的最佳方法是什么?如果我的集合中有6个字段,我的示例中是否需要36个索引? 或者每个索引有一个字段是否更好?

1 个答案:

答案 0 :(得分:0)

这个问题的答案取决于你的集合大小,查询率,不同类型的查询的分布(一些参数的查询比其他参数更高),等等。

如果您收集的文件小于100k(例如)文档,您可以确信所有查询都,即使他们不使用索引并始终进行顺序扫描。

另一方面,如果每秒有1000个这样的查询,它将无法按预期工作,在这种情况下,您必须使用分析器( system.profile )来确定分布不同类型的查询。当你得到这个分布时,很明显,你应该创建哪些索引。

您的主要目标应该是减少您阅读以满足查询的数据量(索引+文档)。

最后,您可以通过创建MongoDB ReplicaSet来增加读取吞吐量。