Mongodb 查询';不要使用合适的索引
我有一个问题Mongodb 查询';不要使用合适的索引,mongodb,mongodb-query,Mongodb,Mongodb Query,我有一个问题 db.Product.find({ CategoryPath: /^399-305-352(-\d+)*$/, "Availability.Status": { $lt: 4 }, $or: [{ _id: { $lt: 331000000 } }, { _id: { $gt: 852000000, $lt:
db.Product.find({
CategoryPath: /^399-305-352(-\d+)*$/,
"Availability.Status": {
$lt: 4
},
$or: [{
_id: {
$lt: 331000000
}
}, {
_id: {
$gt: 852000000,
$lt: 853000000
}
}, {
_id: {
$gt: 972000000,
$lt: 973000000
}
}]
}).sort({
"Availability.Status": 1,
Popularity: -1
});
通过解释,我发现它使用了索引可用性。状态\u 1\u受欢迎程度\u-1
:
{
"cursor" : "BtreeCursor Availability.Status_1_Popularity_-1",
"isMultiKey" : false,
"n" : 913,
"nscannedObjects" : 470239,
"nscanned" : 470239,
"nscannedObjectsAllPlans" : 1387264,
"nscannedAllPlans" : 1387264,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 10838,
"nChunkSkips" : 0,
"millis" : 10117,
"indexBounds" : {
"Availability.Status" : [
[
-Infinity,
4
]
],
"Popularity" : [
[
{
"$maxElement" : 1
},
{
"$minElement" : 1
}
]
]
},
"server" : "dal05mgo13.sl.dx:27017",
"filterSet" : false
}
这对我来说太慢了。实际上,我还有另一个索引CategoryPath\u 1\u Availability.Status\u 1
,我认为这是一个更好的选择。但是当我强制mongodb将其与提示一起使用时,我得到一个错误:
{
"$err" : "Runner error: Overflow sort stage buffered data usage of 33581891 bytes exceeds internal limit of 33554432 bytes",
"code" : 17144
}
现在我不明白的是,在find
中指定的条件下,只选择了913
结果,即使没有索引,sort
也不应该使用32MB内存对913条记录进行排序。有人能告诉我发生了什么事吗
我使用的是MongoDB 2.6.10 x86_64
编辑:我的同事刚刚创建了一个新的索引Availability.Status_1_Popularity_-1_CategoryPath_1
,该索引目前正从其他计划中获胜。我还是不明白为什么。以下是详细的解释信息:
{
"cursor" : "BtreeCursor Availability.Status_1_Popularity_-1_CategoryPath_1",
"isMultiKey" : true,
"n" : 913,
"nscannedObjects" : 1325,
"nscanned" : 1930,
"nscannedObjectsAllPlans" : 7729,
"nscannedAllPlans" : 8334,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 64,
"nChunkSkips" : 0,
"millis" : 45,
"indexBounds" : {
"Availability.Status" : [
[
-Infinity,
4
]
],
"Popularity" : [
[
{
"$maxElement" : 1
},
{
"$minElement" : 1
}
]
],
"CategoryPath" : [
[
"399-305-352",
"399-305-353"
],
[
/^399-305-352(-\d+)*$/,
/^399-305-352(-\d+)*$/
]
]
},
"allPlans" : [
{
"cursor" : "BtreeCursor Availability.Status_1_Popularity_-1_CategoryPath_1",
"isMultiKey" : true,
"n" : 913,
"nscannedObjects" : 1325,
"nscanned" : 1930,
"scanAndOrder" : false,
"indexOnly" : false,
"nChunkSkips" : 0,
"indexBounds" : {
"Availability.Status" : [
[
-Infinity,
4
]
],
"Popularity" : [
[
{
"$maxElement" : 1
},
{
"$minElement" : 1
}
]
],
"CategoryPath" : [
[
"399-305-352",
"399-305-353"
],
[
/^399-305-352(-\d+)*$/,
/^399-305-352(-\d+)*$/
]
]
}
},
{
"cursor" : "BtreeCursor Availability.Status_1_Popularity_-1",
"isMultiKey" : false,
"n" : 0,
"nscannedObjects" : 306,
"nscanned" : 306,
"scanAndOrder" : false,
"indexOnly" : false,
"nChunkSkips" : 0,
"indexBounds" : {
"Availability.Status" : [
[
-Infinity,
4
]
],
"Popularity" : [
[
{
"$maxElement" : 1
},
{
"$minElement" : 1
}
]
]
}
},
{
"clauses" : [
{
"cursor" : "BtreeCursor _id_",
"isMultiKey" : false,
"n" : 305,
"nscannedObjects" : 305,
"nscanned" : 305,
"scanAndOrder" : false,
"indexOnly" : false,
"nChunkSkips" : 0,
"indexBounds" : {
"_id" : [
[
852000000,
853000000
]
]
}
},
{
"cursor" : "BtreeCursor _id_",
"isMultiKey" : false,
"n" : 0,
"nscannedObjects" : 0,
"nscanned" : 0,
"scanAndOrder" : false,
"indexOnly" : false,
"nChunkSkips" : 0,
"indexBounds" : {
"_id" : [
[
972000000,
973000000
]
]
}
},
{
"cursor" : "BtreeCursor _id_",
"isMultiKey" : false,
"n" : 0,
"nscannedObjects" : 0,
"nscanned" : 0,
"scanAndOrder" : false,
"indexOnly" : false,
"nChunkSkips" : 0,
"indexBounds" : {
"_id" : [
[
-Infinity,
331000000
]
]
}
}
],
"cursor" : "QueryOptimizerCursor",
"n" : 0,
"nscannedObjects" : 305,
"nscanned" : 305,
"scanAndOrder" : true,
"nChunkSkips" : 0
},
{
"clauses" : [
{
"cursor" : "BtreeCursor _id_",
"isMultiKey" : false,
"n" : 7,
"nscannedObjects" : 7,
"nscanned" : 7,
"scanAndOrder" : false,
"indexOnly" : false,
"nChunkSkips" : 0,
"indexBounds" : {
"_id" : [
[
972000000,
973000000
]
]
}
},
{
"cursor" : "BtreeCursor _id_1_Availability.Status_1_Popularity_-1",
"isMultiKey" : false,
"n" : 297,
"nscannedObjects" : 297,
"nscanned" : 297,
"scanAndOrder" : false,
"indexOnly" : false,
"nChunkSkips" : 0,
"indexBounds" : {
"_id" : [
[
852000000,
853000000
]
],
"Availability.Status" : [
[
{
"$minElement" : 1
},
{
"$maxElement" : 1
}
]
],
"Popularity" : [
[
{
"$maxElement" : 1
},
{
"$minElement" : 1
}
]
]
}
},
{
"cursor" : "BtreeCursor _id_",
"isMultiKey" : false,
"n" : 0,
"nscannedObjects" : 0,
"nscanned" : 0,
"scanAndOrder" : false,
"indexOnly" : false,
"nChunkSkips" : 0,
"indexBounds" : {
"_id" : [
[
-Infinity,
331000000
]
]
}
}
],
"cursor" : "QueryOptimizerCursor",
"n" : 0,
"nscannedObjects" : 304,
"nscanned" : 304,
"scanAndOrder" : true,
"nChunkSkips" : 0
},
{
"clauses" : [
{
"cursor" : "BtreeCursor _id_",
"isMultiKey" : false,
"n" : 305,
"nscannedObjects" : 305,
"nscanned" : 305,
"scanAndOrder" : false,
"indexOnly" : false,
"nChunkSkips" : 0,
"indexBounds" : {
"_id" : [
[
852000000,
853000000
]
]
}
},
{
"cursor" : "BtreeCursor _id_1_Availability.Status_1_Popularity_-1",
"isMultiKey" : false,
"n" : 0,
"nscannedObjects" : 0,
"nscanned" : 0,
"scanAndOrder" : false,
"indexOnly" : false,
"nChunkSkips" : 0,
"indexBounds" : {
"_id" : [
[
972000000,
973000000
]
],
"Availability.Status" : [
[
{
"$minElement" : 1
},
{
"$maxElement" : 1
}
]
],
"Popularity" : [
[
{
"$maxElement" : 1
},
{
"$minElement" : 1
}
]
]
}
},
{
"cursor" : "BtreeCursor _id_",
"isMultiKey" : false,
"n" : 0,
"nscannedObjects" : 0,
"nscanned" : 0,
"scanAndOrder" : false,
"indexOnly" : false,
"nChunkSkips" : 0,
"indexBounds" : {
"_id" : [
[
-Infinity,
331000000
]
]
}
}
],
"cursor" : "QueryOptimizerCursor",
"n" : 0,
"nscannedObjects" : 305,
"nscanned" : 305,
"scanAndOrder" : true,
"nChunkSkips" : 0
},
{
"clauses" : [
{
"cursor" : "BtreeCursor _id_1_Availability.Status_1_Popularity_-1",
"isMultiKey" : false,
"n" : 305,
"nscannedObjects" : 305,
"nscanned" : 305,
"scanAndOrder" : false,
"indexOnly" : false,
"nChunkSkips" : 0,
"indexBounds" : {
"_id" : [
[
852000000,
853000000
]
],
"Availability.Status" : [
[
{
"$minElement" : 1
},
{
"$maxElement" : 1
}
]
],
"Popularity" : [
[
{
"$maxElement" : 1
},
{
"$minElement" : 1
}
]
]
}
},
{
"cursor" : "BtreeCursor _id_1_Availability.Status_1_Popularity_-1",
"isMultiKey" : false,
"n" : 0,
"nscannedObjects" : 0,
"nscanned" : 0,
"scanAndOrder" : false,
"indexOnly" : false,
"nChunkSkips" : 0,
"indexBounds" : {
"_id" : [
[
972000000,
973000000
]
],
"Availability.Status" : [
[
{
"$minElement" : 1
},
{
"$maxElement" : 1
}
]
],
"Popularity" : [
[
{
"$maxElement" : 1
},
{
"$minElement" : 1
}
]
]
}
},
{
"cursor" : "BtreeCursor _id_",
"isMultiKey" : false,
"n" : 0,
"nscannedObjects" : 0,
"nscanned" : 0,
"scanAndOrder" : false,
"indexOnly" : false,
"nChunkSkips" : 0,
"indexBounds" : {
"_id" : [
[
-Infinity,
331000000
]
]
}
}
],
"cursor" : "QueryOptimizerCursor",
"n" : 0,
"nscannedObjects" : 305,
"nscanned" : 305,
"scanAndOrder" : true,
"nChunkSkips" : 0
},
{
"clauses" : [
{
"cursor" : "BtreeCursor _id_",
"isMultiKey" : false,
"n" : 305,
"nscannedObjects" : 305,
"nscanned" : 305,
"scanAndOrder" : false,
"indexOnly" : false,
"nChunkSkips" : 0,
"indexBounds" : {
"_id" : [
[
852000000,
853000000
]
]
}
},
{
"cursor" : "BtreeCursor _id_",
"isMultiKey" : false,
"n" : 0,
"nscannedObjects" : 0,
"nscanned" : 0,
"scanAndOrder" : false,
"indexOnly" : false,
"nChunkSkips" : 0,
"indexBounds" : {
"_id" : [
[
972000000,
973000000
]
]
}
},
{
"cursor" : "BtreeCursor _id_1_Availability.Status_1_Popularity_-1",
"isMultiKey" : false,
"n" : 0,
"nscannedObjects" : 0,
"nscanned" : 0,
"scanAndOrder" : false,
"indexOnly" : false,
"nChunkSkips" : 0,
"indexBounds" : {
"_id" : [
[
-Infinity,
331000000
]
],
"Availability.Status" : [
[
{
"$minElement" : 1
},
{
"$maxElement" : 1
}
]
],
"Popularity" : [
[
{
"$maxElement" : 1
},
{
"$minElement" : 1
}
]
]
}
}
],
"cursor" : "QueryOptimizerCursor",
"n" : 0,
"nscannedObjects" : 305,
"nscanned" : 305,
"scanAndOrder" : true,
"nChunkSkips" : 0
}
],
"server" : "dal05mgo12.sl.dx:27017",
"filterSet" : false,
"stats" : {
"type" : "FETCH",
"works" : 1931,
"yields" : 64,
"unyields" : 64,
"invalidates" : 0,
"advanced" : 913,
"needTime" : 1017,
"needFetch" : 0,
"isEOF" : 1,
"alreadyHasObj" : 0,
"forcedFetches" : 0,
"matchTested" : 913,
"children" : [
{
"type" : "IXSCAN",
"works" : 1931,
"yields" : 64,
"unyields" : 64,
"invalidates" : 0,
"advanced" : 1325,
"needTime" : 605,
"needFetch" : 0,
"isEOF" : 1,
"keyPattern" : "{ Availability.Status: 1.0, Popularity: -1.0, CategoryPath: 1.0 }",
"isMultiKey" : 1,
"boundsVerbose" : "field #0['Availability.Status']: [-inf.0, 4.0), field #1['Popularity']: [MaxKey, MinKey], field #2['CategoryPath']: [\"399-305-352\", \"399-305-353\"), [/^399-305-352(-\\d+)*$/, /^399-305-352(-\\d+)*$/]",
"yieldMovedCursor" : 0,
"dupsTested" : 1325,
"dupsDropped" : 0,
"seenInvalidated" : 0,
"matchTested" : 0,
"keysExamined" : 1930,
"children" : [ ]
}
]
}
}
这是因为索引中字段的顺序很重要
CategoryPath_1_Availability.Status_1转换为以下索引顺序:按CategoryPath索引,然后按Availability.Status索引
但是您希望索引以Availability.Status开头,mongo会检查是否有以Availability.Status开头的索引(升序),而没有(在您的朋友创建新索引之前)。订购事宜:)
作为一般规则:假设集合中有以下字段:a、b和c。让我们在a和b上设置一个索引(a递增,b递增)仅当排序以a(升序)、或a(升序)和b(升序)开始时,才能在排序中使用此索引。希望您能理解:)向我们展示。提示(“您的索引定义”)。解释(1)
以便我们能看到全部被拒绝的计划。@BlakesSeven那里的索引太多了,我必须先删除一些无意义的索引,然后再在这里发布。请稍等。@BlakesSeven实际上我不明白的是,mongodb会在过滤之前对文档进行排序吗?这是发生溢出的唯一可能性。除非您完全确定,否则请不要删除,因为您是提出问题的人,因此最好不要删除。但是“太多的标记”已经让我感到害怕了,听起来似乎有很多标记被定义了,可能不应该存在。这就是为什么至少“我”希望看到完整的解释“详细”输出,因为看起来根本没有选择索引进行处理,或者它被拒绝至少有一个强有力的理由。这里有一个问题,正如你所看到的,只有900多个文档被选中。为什么mongodb不使用我的索引和排序而不使用索引?这样做会占用更少的资源。当我用索引提示时,内存溢出就来了。唯一的解释是mongodb在过滤之前进行排序。这是我不理解的部分。那是因为你要求mongo为该查询使用错误的索引。是的,我知道。我的意思是,mongo可以使用我提供的索引,在没有索引的情况下进行排序。我认为在内存中排序900条记录并不是什么大问题。不知何故,mongo决定在筛选之前对所有500000条记录进行排序。这是我不明白的部分。事实上,mongo首先应用排序,然后应用过滤器,这就是它的工作原理