【发布时间】:2015-05-13 11:14:56
【问题描述】:
这不是一个重复的问题。所有其他答案都说解决方案是在排序键上创建索引。就我而言,我确实有一个索引,但仍然面临这个错误
给定一个 mongodb 集合,其文档类似于:
{
'_id': ...,
'title': ...,
'price': ...,
'category_id': ...,
'last_updated': ...,
... other keys
}
category_id 上有一个升序单字段索引,last_updated 有一个降序单字段索引。
以下查询崩溃:
> var c = db.collection_name.find({category_id: "categ_id"}, {_id: 0, price: 1, title: 1}).sort({last_updated: -1}).limit(20000).batchSize(500)
> c.forEach(function(doc) {
... ;
... })
2015-05-13T10:00:46.561+0000 E QUERY Error: error: {
"$err" : "getMore executor error: Overflow sort stage buffered data usage of 33554596 bytes exceeds internal limit of 33554432 bytes",
"code" : 17406
}
at Error (<anonymous>)
at DBQuery.next (src/mongo/shell/query.js:259:15)
at DBQuery.forEach (src/mongo/shell/query.js:414:20)
at (shell):1:3 at src/mongo/shell/query.js:259
如果有帮助,这里是查询的解释:
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "db_name.collection_name",
"indexFilterSet" : false,
"parsedQuery" : {
"category_id" : {
"$eq" : "categ_id"
}
},
"winningPlan" : {
"stage" : "PROJECTION",
"transformBy" : {
"_id" : 0,
"price" : 1,
"title" : 1
},
"inputStage" : {
"stage" : "SORT",
"sortPattern" : {
"last_updated" : -1
},
"limitAmount" : 500,
"inputStage" : {
"stage" : "KEEP_MUTATIONS",
"inputStage" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"category_id" : 1
},
"indexName" : "category_id_1",
"isMultiKey" : false,
"direction" : "forward",
"indexBounds" : {
"category_id" : [
"[\"categ_id\", \"categ_id\"]"
]
}
}
}
}
}
},
"rejectedPlans" : [
{
"stage" : "LIMIT",
"limitAmount" : 500,
"inputStage" : {
"stage" : "PROJECTION",
"transformBy" : {
"_id" : 0,
"price" : 1,
"title" : 1
},
"inputStage" : {
"stage" : "FETCH",
"filter" : {
"category_id" : {
"$eq" : "categ_id"
}
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"last_updated" : 1
},
"indexName" : "last_updated_1",
"isMultiKey" : false,
"direction" : "backward",
"indexBounds" : {
"last_updated" : [
"[MaxKey, MinKey]"
]
}
}
}
}
}
]
},
"serverInfo" : {
"host" : "host",
"port" : 27017,
"version" : "3.0.2",
"gitVersion" : "6201872043ecbbc0a4cc169b5482dcf385fc464f"
},
"ok" : 1
}
有趣的是,此错误仅发生在特定类别而不是全部。此外,如果我删除 batchSize 选项,查询不会崩溃(不管我为批处理设置的大小)。
值得注意的是last_updated字段可能并不存在于所有文档中。
【问题讨论】:
标签: mongodb mongodb-query