gpt4 book ai didi

mongodb - 为什么 Mongo 提示会使查询运行速度提高 10 倍?

转载 作者:IT老高 更新时间:2023-10-28 13:05:02 25 4
gpt4 key购买 nike

如果我使用 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) 运行

最佳答案

Mongo 使用一种算法来确定在未提供提示时使用哪个索引,然后缓存用于类似查询的索引以供接下来的 1000 次调用

但是每当你解释一个 mongo 查询时,它总是会运行索引选择算法,因此与没有提示的 explain() 相比,带有提示的 explain() 总是花费更少的时间。

这里回答了类似的问题 Understanding mongo db explain

关于mongodb - 为什么 Mongo 提示会使查询运行速度提高 10 倍?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7730591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com