MongoDB排序內存不夠問題

問題:mongodb

綜合查詢處理異常:
Query failed with error code 96 and error message 'Executor error during find command :: caused by :: errmsg: "Sort operation used more than the maximum 33554432 bytes of RAM. Add an index, or specify a smaller limit."' on server 10.1.1.155:27017; nested exception is com.mongodb.MongoQueryException: Query failed with error code 96 and error message 'Executor error during find command :: caused by :: errmsg: "Sort operation used more than the maximum 33554432 bytes of RAM. Add an index, or specify a smaller limit."' on server 10.1.1.155:27017

今天遇到一個問題,使用MongoDB進行分頁查詢的時候,因爲數據量比較大,每頁顯示10條數據,到5000頁左右的時候會報上面的錯誤,很明顯,使用skip+limit在小數據量的時候沒有問題,當數據量比較大時,MongoDB因爲內部機制限制Sort的最大內存爲32M,當排序的數據量超過32M就會報如上錯誤,解決上面問題有兩種方法,一種就是增長排序的內存,另外一種就是對排序的字段建立索引。 1、從新分配更大的排序內存(320M):code

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

2、爲排序的字段建立索引server

db.getCollection("base_person").createIndex({"orgCode":1})
相關文章
相關標籤/搜索