MongoDB內存溢出錯誤描述
MongoDB內存中排序的限制和解決方案
下文引用自:https://docs.mongodb.com/manual/reference/method/cursor.sort/#cursor.sort
When unable to obtain the sort order from an index, MongoDB will sort the results in memory, which requires that the result set being sorted is less than 32 megabytes.
When the sort operation consumes more than 32 megabytes, MongoDB returns an error. To avoid this error, either create an index supporting the sort operation (see Sort and Index Use) or use sort() in conjunction with limit() (see Limit Results).
MongoDB查詢方法的描述和執行順序
下文引用自:https://docs.mongodb.com/manual/tutorial/query-documents/#query-method
Query Method
MongoDB provides the db.collection.find() method to read documents from a collection. The db.collection.find() method returns a cursor to the matching documents.
For the db.collection.find() method, you can specify the following optional fields:
a query filter to specify which documents to return.
a query projection to specifies which fields from the matching documents to return. The projection limits the amount of data that MongoDB returns to the client over the network.
You can optionally add a cursor modifier to impose limits, skips, and sort orders. The order of documents returned by a query is not defined unless you specify a sort().
下文引用自:https://docs.mongodb.com/manual/reference/method/db.collection.find/#combine-cursor-methods
Combine Cursor Methods
The following statements chain cursor methods limit() and sort():
The two statements are equivalent; i.e. the order in which you chain the limit() and the sort() methods is not significant. Both statements return the first five documents, as determined by the ascending sort order on ‘name’.
順便來看看SQL Server語句執行順序
《SQL Server 2005技術內幕--查詢》這本書的開篇第一章第一節。書的作者也要讓讀者首先了解語句是怎么樣的一個執行順序。
查詢的邏輯執行順序:
(1) FROM < left_table>
(3) < join_type> JOIN < right_table> (2) ON < join_condition>
(4) WHERE < where_condition>
(5) GROUP BY < group_by_list>
(6) WITH {cube | rollup}
(7) HAVING < having_condition>
(8) SELECT (9) DISTINCT (11) < top_specification> < select_list>
(10) ORDER BY < order_by_list>
標準的SQL 的解析順序為:
(1).FROM 子句 組裝來自不同數據源的數據
(2).WHERE 子句 基于指定的條件對記錄進行篩選
(3).GROUP BY 子句 將數據劃分為多個分組
(4).使用聚合函數進行計算
(5).使用HAVING子句篩選分組
(6).計算所有的表達式
(7).使用ORDER BY對結果集進行排序
執行順序:
1.FROM:對FROM子句中前兩個表執行笛卡爾積生成虛擬表vt1
2.ON:對vt1表應用ON篩選器只有滿足< join_condition> 為真的行才被插入vt2
3.OUTER(join):如果指定了 OUTER JOIN保留表(preserved table)中未找到的行將行作為外部行添加到vt2 生成t3如果from包含兩個以上表則對上一個聯結生成的結果表和下一個表重復執行步驟和步驟直接結束
4.WHERE:對vt3應用 WHERE 篩選器只有使< where_condition> 為true的行才被插入vt4
5.GROUP BY:按GROUP BY子句中的列列表對vt4中的行分組生成vt5
6.CUBE|ROLLUP:把超組(supergroups)插入vt6 生成vt6
7.HAVING:對vt6應用HAVING篩選器只有使< having_condition> 為true的組才插入vt7
8.SELECT:處理select列表產生vt8
9.DISTINCT:將重復的行從vt8中去除產生vt9
10.ORDER BY:將vt9的行按order by子句中的列列表排序生成一個游標vc10
11.TOP:從vc10的開始處選擇指定數量或比例的行生成vt11 并返回調用者
對比總結
MongoDB和SQL Server都是先SELECT列表后,再到內存中排序,最后取前幾行。
對于內存溢出的優化
MongoDB查詢優化的原則可參考:
Optimize Query Performance
https://docs.mongodb.com/manual/tutorial/optimize-query-performance-with-indexes-and-projections/
有的開發會干脆將數據取出來后在程序里排序,這個不推薦,因為這樣同樣占用過多內存,沒有從根本上解決這個問題。
比較推薦的方案有三個:
1.優化查詢和索引。
2.減少輸出列(限制輸出列個數)或行(如limit函數,或限制輸入查詢_id數量)。
3.將查詢分2步,第1步只輸出_id,第2步再通過_id查明細。
都可以解決內存中排序溢出問題。
從3.0版本開始的系統參數調優
從3.0版本開始可以通過修改參數值internalQueryExecMaxBlockingSortBytes來增加內存排序大小限制。
先來看看所有支持的參數:
use admin db.runCommand( { getParameter : 1, "internalQueryExecMaxBlockingSortBytes" : 1 } )再來看看如何設置:
db.adminCommand({setParameter: 1, internalQueryExecMaxBlockingSortBytes: <limit in bytes>})另外有需要云服務器可以了解下創新互聯cdcxhl.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
分享文章:深入MongoDB內存溢出調優-創新互聯
標題URL:http://vcdvsql.cn/article48/pgiep.html
成都網站建設公司_創新互聯,為您提供云服務器、靜態網站、電子商務、企業建站、網站改版、商城網站
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯