用户在查询时
查看日志报错内容是:
[Error] 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.
at line 0, column 0
原因显而易见,已经在报错里面说的比较清楚了,查询mongodb文档发现
If MongoDB cannot use an index or indexes to obtain the sort order, MongoDB must > perform an in-memory sort operation on the data.
那解决方案有两种
- 增加index能使用的内存
- 创建索引
先在admin数据库下查询一下目前索引能使用的内存是多少,在mongodb4.3及以下版本中,使用internalQueryExecMaxBlockingSortBytes
参数存储索引内存,执行:
db.runCommand({
getParameter: 1,
"internalQueryExecMaxBlockingSortBytes": 1
})
查询internalQueryExecMaxBlockingSortBytes
结果返回:
{
"internalQueryExecMaxBlockingSortBytes" : 33554432,
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1753329460, 1),
"signature" : {
"hash" : BinData(0,"3axx/tC1CMR0BapEgOC1goe1wt8="),
"keyId" : 7467435427377446915
}
},
"operationTime" : Timestamp(1753329460, 1)
}
注意对于mongodb4.3以上的版本,官方更新了参数名称为internalQueryMaxBlockingSortMemoryUsageBytes
,使用以下查询语句查询索引允许使用的最大内存:
db.runCommand({
getParameter: 1,
"internalQueryMaxBlockingSortMemoryUsageBytes": 1
})
修改这个参数的值即可
如果是创建索引,执行
db.CollectionName.createIndex({
"A": 1
});
CollectionName替换为目标Collection