假设一个collection tygzJyrzVO,有7512条数据
数据长这样子的
试图排序后拿前10条数据
报排序超过了最大内存限制异常。报错的完整信息如下
Error: error: { "operationTime" : Timestamp(1569469310, 2), "ok" : 0, "errmsg" : "Executor error during find command :: caused by :: Sort operation used more than the maximum 33554432 bytes of RAM. Add an index, or specify a smaller limit.", "code" : 96, "codeName" : "OperationFailed", "$clusterTime" : { "clusterTime" : Timestamp(1569469310, 2), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } }}
提示也非常明显:增加索引或者指定更小的limit
减小limit
expalin limit(8)的时候,看下查询分析数据 db.tygzJyrzVO.find().sort({lrrq:-1}).limit(8).explain("allPlansExecution");
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "taxcp_dev.tygzJyrzVO",
"indexFilterSet" : false,
"parsedQuery" : {
},
"winningPlan" : {
"stage" : "SORT",
"sortPattern" : {
"lrrq" : -1
},
"limitAmount" : 8,
"inputStage" : {
"stage" : "SORT_KEY_GENERATOR",
"inputStage" : {
"stage" : "COLLSCAN",
"direction" : "forward"
}
}
},
"rejectedPlans" : [ ]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 8,
"executionTimeMillis" : 138,
"totalKeysExamined" : 0,
"totalDocsExamined" : 7512,
"executionStages" : {
"stage" : "SORT",
"nReturned" : 8,
"executionTimeMillisEstimate" : 106,
"works" : 7524,
"advanced" : 8,
"needTime" : 7515,
"needYield" : 0,
"saveState" : 60,
"restoreState" : 60,
"isEOF" : 1,
"invalidates" : 0,
"sortPattern" : {
"lrrq" : -1
},
"memUsage" : 19217,
"memLimit" : 33554432,
"limitAmount" : 8,
"inputStage" : {
"stage" : "SORT_KEY_GENERATOR",
"nReturned" : 7512,
"executionTimeMillisEstimate" : 5,
"works" : 7515,
"advanced" : 7512,
"needTime" : 2,
"needYield" : 0,
"saveState" : 60,
"restoreState" : 60,
"isEOF" : 1,
"invalidates" : 0,
"inputStage" : {
"stage" : "COLLSCAN",
"nReturned" : 7512,
"executionTimeMillisEstimate" : 2,
"works" : 7514,
"advanced" : 7512,
"needTime" : 1,
"needYield" : 0,
"saveState" : 60,
"restoreState" : 60,
"isEOF" : 1,
"invalidates" : 0,
"direction" : "forward",
"docsExamined" : 7512
}
}
},
"allPlansExecution" : [ ]
},
"serverInfo" : {
"host" : "dyh231",
"port" : 27017,
"version" : "4.0.11",
"gitVersion" : "417d1a712e9f040d54beca8e4943edce218e9a8c"
},
"ok" : 1,
"operationTime" : Timestamp(1569470061, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1569470061, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
expalin limit(10)的时候 ,db.tygzJyrzVO.find().sort({lrrq:-1}).limit(10).explain("allPlansExecution");
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "taxcp_dev.tygzJyrzVO",
"indexFilterSet" : false,
"parsedQuery" : {
},
"winningPlan" : {
"stage" : "SORT",
"sortPattern" : {
"lrrq" : -1
},
"limitAmount" : 10,
"inputStage" : {
"stage" : "SORT_KEY_GENERATOR",
"inputStage" : {
"stage" : "COLLSCAN",
"direction" : "forward"
}
}
},
"rejectedPlans" : [ ]
},
"executionStats" : {
"executionSuccess" : false,
"errorMessage" : "Exec error resulting in state FAILURE :: caused by :: Sort operation used more than the maximum 33554432 bytes of RAM. Add an index, or specify a smaller limit.",
"errorCode" : 96,
"nReturned" : 0,
"executionTimeMillis" : 115,
"totalKeysExamined" : 0,
"totalDocsExamined" : 7080,
"executionStages" : {
"stage" : "SORT",
"nReturned" : 0,
"executionTimeMillisEstimate" : 112,
"works" : 7083,
"advanced" : 0,
"needTime" : 7082,
"needYield" : 0,
"saveState" : 56,
"restoreState" : 56,
"isEOF" : 0,
"invalidates" : 0,
"sortPattern" : {
"lrrq" : -1
},
"memUsage" : 36125389,
"memLimit" : 33554432,
"limitAmount" : 10,
"inputStage" : {
"stage" : "SORT_KEY_GENERATOR",
"nReturned" : 7080,
"executionTimeMillisEstimate" : 6,
"works" : 7082,
"advanced" : 7080,
"needTime" : 2,
"needYield" : 0,
"saveState" : 56,
"restoreState" : 56,
"isEOF" : 0,
"invalidates" : 0,
"inputStage" : {
"stage" : "COLLSCAN",
"nReturned" : 7080,
"executionTimeMillisEstimate" : 0,
"works" : 7081,
"advanced" : 7080,
"needTime" : 1,
"needYield" : 0,
"saveState" : 56,
"restoreState" : 56,
"isEOF" : 0,
"invalidates" : 0,
"direction" : "forward",
"docsExamined" : 7080
}
}
},
"allPlansExecution" : [ ]
},
"serverInfo" : {
"host" : "dyh231",
"port" : 27017,
"version" : "4.0.11",
"gitVersion" : "417d1a712e9f040d54beca8e4943edce218e9a8c"
},
"ok" : 1,
"operationTime" : Timestamp(1569470101, 722),
"$clusterTime" : {
"clusterTime" : Timestamp(1569470101, 722),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
说明,在排序过程中,limit(10)无法获取所有collection记录来进行排序就排序内存大小超限了(32MB),而limit(8)的时候排序成功了。
建立索引
db.tygzJyrzVO.ensureIndex({lrrq:-1},{background:true});
再看 db.tygzJyrzVO.find().sort({lrrq:-1}).limit(10).explain("allPlansExecution");
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "taxcp_dev.tygzJyrzVO",
"indexFilterSet" : false,
"parsedQuery" : {
},
"winningPlan" : {
"stage" : "LIMIT",
"limitAmount" : 10,
"inputStage" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"lrrq" : -1
},
"indexName" : "lrrq_-1",
"isMultiKey" : false,
"multiKeyPaths" : {
"lrrq" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"lrrq" : [
"[MaxKey, MinKey]"
]
}
}
}
},
"rejectedPlans" : [ ]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 10,
"executionTimeMillis" : 0,
"totalKeysExamined" : 10,
"totalDocsExamined" : 10,
"executionStages" : {
"stage" : "LIMIT",
"nReturned" : 10,
"executionTimeMillisEstimate" : 0,
"works" : 11,
"advanced" : 10,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"limitAmount" : 10,
"inputStage" : {
"stage" : "FETCH",
"nReturned" : 10,
"executionTimeMillisEstimate" : 0,
"works" : 10,
"advanced" : 10,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 0,
"invalidates" : 0,
"docsExamined" : 10,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 10,
"executionTimeMillisEstimate" : 0,
"works" : 10,
"advanced" : 10,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 0,
"invalidates" : 0,
"keyPattern" : {
"lrrq" : -1
},
"indexName" : "lrrq_-1",
"isMultiKey" : false,
"multiKeyPaths" : {
"lrrq" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"lrrq" : [
"[MaxKey, MinKey]"
]
},
"keysExamined" : 10,
"seeks" : 1,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
},
"allPlansExecution" : [ ]
},
"serverInfo" : {
"host" : "dyh231",
"port" : 27017,
"version" : "4.0.11",
"gitVersion" : "417d1a712e9f040d54beca8e4943edce218e9a8c"
},
"ok" : 1,
"operationTime" : Timestamp(1569478794, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1569478794, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
建立索引后,好了,在内存进行排序操作都避免了(memLimit,memUsage没有了)。效果提升很明显。