zoukankan      html  css  js  c++  java
  • MongoDB 查询时排序超过内存限制的问题

    org.springframework.data.mongodb.UncategorizedMongoDbException: Query failed with error code 17144 and error message 'Executor error: Overflow sort stage buffered data usage of 33554898 bytes exceeds internal limit of 33554432 bytes' on server 127.0.0.1:27017; nested exception is com.mongodb.MongoQueryException: Query failed with error code 17144 and error message 'Executor error: Overflow sort stage buffered data usage of 33554898 bytes exceeds internal limit of 33554432 bytes' on server 127.0.0.1:27017
        at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:107)
        at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:2114)
        at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:1957)
        at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:1763)
        at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:1746)
        at org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:624)
        at com.controller.TestController.getList(TestController.java:1216)
        at com..controller.TestController$$FastClassBySpringCGLIB$$d9a15731.invoke(<generated>)

    报错原因:在排序字段未利用到索引的情况下,若超过32M内存则会被Abort,语句直接返回报错

    Sort operation used more than the maximum 33554432 bytes of RAM.,33554432 bytes算下来正好是32Mb,而mongodb的sort操作是把数据拿到内存中再进行排序的,为了节约内存,默认给sort操作限制了最大内存为32Mb,当数据量越来越大直到超过32Mb的时候就抛出异常了!

    解决方式:给具体要对哪个字段加索引,可根据需求自己定义,其中1表示升序排列,-1表示降序排列。

    MongoDB排序方式:MongoDB可以使用索引扫描来进行排序,那么结果将不包括SORT stage。否则如果MongoDB无法使用索引进行排序,那么查询计划将包括SORT stage。

                                               使用索引扫描的效率是远大于直接将结果集放在内存排序的,所以MongoDB为了使查询语句更有效率的执行,限制了 排序内存的使用,因而规定了只能使用 32M。

                                                注意保持查询中组合排序的升降序和组合索引中的 方向 保持 全部相同 或 全部相反

    索引操作语句

    db.getCollection('document').getIndexes()      查看当前索引

    db.getCollection('document').createIndex({"age": 1})    创建单个索引

    db.getCollection('document').createIndex({"age": 1,"name":1},{"name":"user"})     一个索引名称下包含多个key

    db.getCollection('document').dropIndex("age")       删除索引

    db.getCollection('document').getIndexes()    //查询document下的索引
    [ {
    "v" : 1, "key" : { "id" : 1 }, "name" : "_id", "ns" : "document" }, { "v" : 1, "key" : { "name" : 1, "age" : 1 }, "name" : "user", "ns" : "document" } ]
  • 相关阅读:
    vue自动路由-单页面项目(非build时构建)
    建立多页面vue.js项目
    C#调用C++(QT5.5.1项目)的C++/CLI(CLR项目)项目技术笔记
    自建Socket转发,使用远程桌面(mstsc)连接家中电脑
    用Vue.js搭建一个小说阅读网站
    在CentOS中部署.Net Core2.1网站
    高价值干货:这可能是你见过最全的网络爬虫总结
    【DevCloud·敏捷智库】如何利用用户故事了解需求
    项目管理:如何显性管理并提升Story分解能力
    【API进阶之路】老板给我涨薪30%!如何通过SDK接口搞定千万级流量直播
  • 原文地址:https://www.cnblogs.com/wueryuan/p/13524659.html
Copyright © 2011-2022 走看看