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" } ]

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM