我已经创建了a
的升序和降序索引
db.getCollection('objectlists').createIndex( { a: 1 } )
db.getCollection('objectlists').createIndex( { a: -1 } )
当我在find
功能中使用此索引时,即使在大量数据上它也可以完美工作
db.getCollection('objectlists').find({a: {$gt: 0}})
->立即返回。
但是,当我使用它进行排序时,例如:
db.getCollection('objectlists').find().sort({a: 1})
,我得到:
Error: error: {
"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"
}
我什至尝试添加提示:
db.getCollection('objectlists').find().sort({a: 1}).hint({a: 1});
但是我最终遇到了同样的错误。在运行此查询之前,我也尝试使用ensureIndex()
,但仍然无法解决该错误。有什么问题?我是否误解了索引排序的工作原理?
db.getCollection('objectlists').find().sort({a: 1}).explain()
的输出是
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "App.objectlists",
"indexFilterSet" : false,
"parsedQuery" : {},
"winningPlan" : {
"stage" : "SORT",
"sortPattern" : {
"a" : 1.0
},
"inputStage" : {
"stage" : "SORT_KEY_GENERATOR",
"inputStage" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"a" : 1
},
"indexName" : "a_1",
"isMultiKey" : true,
"multiKeyPaths" : {
"a" : [
"a"
]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"a" : [
"[MinKey, MaxKey]"
]
}
}
}
}
},
"rejectedPlans" : []