上线许久的产品突然爆出了一个Mongodb 查询的BUG,错误如下:

"exception":"org.springframework.data.mongodb.UncategorizedMongoDbException",
"message":"Query failed with error code 96 and error message 'Executor error during find command: OperationFailed: Sort operation used more than the maximum 33554432 bytes of RAM. Add an index, or specify a smaller limit.' on server 127.0.0.1:27017;
nested exception is com.mongodb.MongoQueryException:
Query failed with error code 96 and error message 'Executor error during find command: OperationFailed: Sort operation used more than the maximum 33554432 bytes of RAM.
Add an index, or specify a smaller limit.' on server 127.0.0.1:27017"

原因比较明确:Sort operation used more than the maximum 33554432 bytes of RAM.33554432 bytes算下来正好是32Mb,而Mongodb的sort操作是把数据拿到内存中再进行排序的,为了节约内存,默认给sort操作限制了最大内存为32Mb,当数据量越来越大直到超过32Mb的时候就自然抛出异常了!解决方案有两个思路,一个是既然内存不够用那就修改默认配置多分配点内存空间;一个是像错误提示里面说的那样创建索引。
首先说如何修改默认内存配置,在Mongodb命令行窗口中执行如下命令即可:

db.adminCommand({setParameter:1, internalQueryExecMaxBlockingSortBytes:335544320})

我直接把内存扩大了10倍,变成了320Mb。从这里可以看出,除非你服务器的内存足够大,否则sort占用的内存会成为一个严重的资源消耗!然后是创建索引,也比较简单:

db.yourCollection.createIndex({<field>:<1 or -1>})
db.yourCollection.getIndexes() //查看当前collection的索引

其中1表示升序排列,-1表示降序排列。索引创建之后即时生效,不需要重启数据库和服务器程序,也不需要对原来的数据库查询语句进行修改。创建索引的话也有不好的地方,会导致数据写入变慢,同时Mongodb数据本身占用的存储空间也会变多。不过从查询性能和服务器资源消耗这两方面来看,通过创建索引来解决这个问题还是最佳的方案!

来自: https://blog.csdn.net/cloume/article/details/70767061

Mongodb: Sort operation used more than the maximum 33554432 bytes of RAM的更多相关文章

  1. MongoDB Sort op eration used more than the maximum 33554432 bytes of RAM. Add an index, or speci fy a smaller limit.

    最近在获取mongodb某个集合的数据过程中,在进行排序的过程中报错,具体报错信息如下: Error: error: { , "errmsg" : "Executor e ...

  2. Overflow sort stage buffered data usage of 33554495 bytes exceeds internal limit of 33554432 bytes

    MongoDB执行错误: Overflow sort stage buffered data usage of 33554495 bytes exceeds internal limit of 335 ...

  3. mongodb sort limit和skip用法

    > db.mediaCollection.find().skip().toArray() [ { "_id" : ObjectId("5353463193efef0 ...

  4. mongodb sort

    sort() 方法 要在 MongoDB 中的文档进行排序,需要使用sort()方法. sort() 方法接受一个文档,其中包含的字段列表连同他们的排序顺序.要指定排序顺序1和-1. 1用于升序排列, ...

  5. mongodb Sort排序能够支持的最大内存限制为32M Plan executor error during find: FAILURE

    1.一个比较老的游戏服维护,关服维护后启动时报错 2.看到关于mongodb的报错,于是去查一下mongodb的日志 Plan executor error during find: FAILURE, ...

  6. MongoDB排序异常

    com.mongodb.MongoQueryException: Query failed with error code 96 and error message 'Executor error d ...

  7. Mongodb——文档数据库

    mongodb是一个文档数据库. mongo操作 多个修改操作,但每个修改携带的数据包较小,可操作考虑批量操作.bulkWrite()改善性能. MongoCollection是线程安全的. db.c ...

  8. mongodb报错一例

    开发程序报错信息: Caused by: com.mongodb.MongoException: Executor error: OperationFailed: Sort operation use ...

  9. 一个MongoDB索引走偏的案例及探究分析

    接业务需求,有一个MongoDB的简单查询,太耗时了,执行了 70S 左右,严重影响用户的体验.. 查询代码主要如下: db.duoduologmodel.find({"Tags.SN&qu ...

随机推荐

  1. Echarts官网展示

    1.参考实例 http://echarts.baidu.com/examples/ 点击去的效果: 2.配置项手册 http://echarts.baidu.com/option.html#title ...

  2. miniui中可以设置是否让页面进行分页 <div id="datagrid1" class="mini-datagrid" style="width:100%" allowAlternating="true" showpager="true"/> 就是设置showpager属性为true

    <div id="datagrid1" class="mini-datagrid" style="width:100%" allowA ...

  3. ATcoder Big Array

    Problem Statement There is an empty array. The following N operations will be performed to insert in ...

  4. Spring AOP 切面编程实战Demo项目

    为什么会有此项目?在某日,我看博客时,看到了讲面向切面编程的内容,之前也知道spring是面向切面编程的,只是自己没有写过相关的代码,于是决定自己写一个test.但是url拦截器从外部看,和AOP有相 ...

  5. SQL 自定义四舍五入

    --============================================== -- 自定义的四舍五入(四舍五入后的所有尾数遇进则进) -- by 小天使 2015-11-12 -- ...

  6. XamarinSQLite教程下载安装SQLite/SQL Server Compact Toolbox

    XamarinSQLite教程下载安装SQLite/SQL Server Compact Toolbox SQLite/SQL Server Compact Toolbox是一个Visual Stud ...

  7. 【DWM1000】 code 解密9一 ANCHOR response poll message

    根据上面TAG发送的代码,我直接找到如下代码 case RTLS_DEMO_MSG_TAG_POLL: { if(inst->mode == LISTENER)                  ...

  8. [BZOJ1814]Formula 1

    Description: 一个 m * n 的棋盘,有的格子存在障碍,求经过所有非障碍格子的哈密顿回路个数 Hint: \(n,m<=12\) Solution: 插头dp模板题,注意要讨论多种 ...

  9. C++学习笔记56:异常处理

    异常处理 异常处理的语法 抛掷异常的程序段 throw表达式: 捕获并处理异常的程序段 try 复合语句 catch(异常声明) 复合语句 catch(异常声明) 复合语句 注意:如果匹配的处理器没有 ...

  10. MySQL JDBC简单使用

    首先需要去MySQL官网下载MySQL JDBC驱动 导入jar包 String driver = "com.mysql.jdbc.Driver"; String url = &q ...