使用Mongo 3.2。MongoDB - 与查询不同,不使用索引
比方说,我有这个模式的集合:
{ _id: 1, type: a, source: x },
{ _id: 2, type: a, source: y },
{ _id: 3, type: b, source: x },
{ _id: 4, type: b, source: y }
当然,我的db是大得多,还有更多的类型和来源。
我已经创建了4个指标种类和来源的组合(即使1应该是足够了):
{type: 1}
{source: 1},
{type: 1, source: 1},
{source: 1, type: 1}
现在,我正在此不同的查询:
db.test.distinct("source", {type: "a"})
的问题是,这个查询需要花费更多的时间。 如果我runCommand运行:
db.runCommand({distinct: 'test', key: "source", query: {type: "a"}})
这是结果我得到:
{
"waitedMS": 0,
"values": [
"x",
"y"
],
"stats": {
"n": 19400840,
"nscanned": 19400840,
"nscannedObjects": 19400840,
"timems": 14821,
"planSummary": "IXSCAN { type: 1 }"
},
"ok": 1
}
出于某种原因,蒙戈只使用type: 1
索引进行查询的阶段。 它应该使用索引也为不同阶段。 这是为什么?使用{type: 1, source: 1}
指数会好很多,不是吗?现在它正在扫描所有type: a
文档,同时它有一个索引。
我做错了什么?对于这种独特的方式,我有更好的选择吗?
来自问题,看起来应该已经在3.4中实现了? –
@Alvin Wong你说得对,谢天谢地这个功能已经在Mongo 3.4中实现了。 – Robert