【发布时间】:2011-12-05 13:07:02
【问题描述】:
如果我使用 explain() 从 shell 运行 mongo 查询,获取使用的索引的名称,然后再次运行相同的查询,但使用 hint() 指定要使用的相同索引 - “millis”字段来自解释计划明显减少
例如
没有提供提示:
>>db.event.find({ "type" : "X", "active" : true, "timestamp" : { "$gte" : NumberLong("1317498259000") }, "count" : { "$gte" : 0 } }).limit(3).sort({"timestamp" : -1 }).explain();
{
"cursor" : "BtreeCursor my_super_index",
"nscanned" : 599,
"nscannedObjects" : 587,
"n" : 3,
"millis" : 24,
"nYields" : 0,
"nChunkSkips" : 0,
"isMultiKey" : true,
"indexOnly" : false,
"indexBounds" : { ... }
}
提供提示:
>>db.event.find({ "type" : "X", "active" : true, "timestamp" : { "$gte" : NumberLong("1317498259000") }, "count" : { "$gte" : 0 } }).limit(3).sort({"timestamp" : -1 }).hint("my_super_index").explain();
{
"cursor" : "BtreeCursor my_super_index",
"nscanned" : 599,
"nscannedObjects" : 587,
"n" : 3,
"millis" : 2,
"nYields" : 0,
"nChunkSkips" : 0,
"isMultiKey" : true,
"indexOnly" : false,
"indexBounds" : { ... }
}
唯一的区别是“毫”字段
有人知道这是为什么吗?
更新:“选择要使用的索引”没有解释它,因为据我所知,mongo 为每个 X(100?)运行选择索引,所以它应该与提示下一个(X -1) 运行
【问题讨论】:
-
要么索引加快了速度,要么你只是重新执行一个缓存查询,它的开销要低得多。
-
我不认为这是因为缓存。如果我在没有提示 2、3 或 10 次的情况下运行相同的查询,它不会快得多,但它总是会随着提示显着加快。
-
您能否编辑您的问题以在没有提示的情况下包含 find(...).explain(true) 的输出。这将打印可能有助于调试的额外信息。
-
确定使用什么索引可能需要 22 毫秒?
-
@wberry 的答案在这里是合适的,带有 hint() 的 explain() 将返回 deniedPlans[] 为空,因为 winsPlan 已经使用 hint() 方法提供了。对于索引数量较多的集合,Delta 将更为重要。
"rejectedPlans" : [ { "stage" : "SKIP", "skipAmount" : 493, "inputStage" : {与"rejectedPlans" : []