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" } ]
  • 相关阅读:
    接口和抽象的区别
    接口
    jquery Ajax提交表单数据
    SQL 检查 是否存在 表 临时表
    ASP.NET MVC 设置Area中 Controller 的方法 默认启动页
    json 序列化为数组
    C# Lamda中类似于SQL 中的 In 功能
    各种webservice调用地址
    ASP.NET获取客户端IP地址
    C#反射机制 Type类型
  • 原文地址:https://www.cnblogs.com/wueryuan/p/13524659.html
Copyright © 2011-2022 走看看