深入MongoDB内存溢出调优
admin
2023-02-06 04:40:03
0

MongoDB内存溢出错误描述

exception: getMore runner error: Overflow sort stage buffered data 
usage of 33638076 bytes exceeds internal limit of 33554432 bytes

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.

db.collection.find(  )


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():

db.bios.find().sort( { name: 1 } ).limit( 5 )
db.bios.find().limit( 5 ).sort( { name: 1 } )

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: })


相关内容

热门资讯

德国总理:美国正在被伊朗羞辱 德国之声4月27日报道,德国总理默茨在访问一所学校时表示,在当前的持续冲突中,伊朗领导层正试图羞辱美...
理响中国|“长”歌以行,风云激... 光阴如梭,东方潮阔。这里是中国的长三角,世界的长三角。无论过去、现在还是未来,这片土地都因时代而生,...
白宫:特朗普及其国安团队开会讨... 新华社华盛顿4月27日电 美国白宫新闻秘书莱维特27日在记者会上证实,总统特朗普及其国家安全团队当天...
人民日报刊文:日本放开杀伤性武... 日本放开杀伤性武器出口推高地缘冲突风险(国际论坛)常思纯《人民日报》(2026年04月28日 第 0...
医疗保障法草案二审:明确生育保... 满足多样化健康保障需求本报记者 彭 波4月27日,医疗保障法草案二审稿提请十四届全国人大常委会第二十...
天津一景区发生自转旋翼机事故1... 澎湃新闻记者 吕新文中国民用航空华北地区管理局4月22日公布《豪客通航“10•1”天津长芦汉盐旅游区...
卡塔尔埃米尔与美国总统特朗普通... 当地时间24日,卡塔尔埃米尔塔米姆与美国总统特朗普通电话,重点就中东地区局势以及伊朗与美国谈判问题交...
男子30年前被扣押2859克黄... 澎湃新闻记者 王鑫家住辽宁省大连市的潘永嘉近日向澎湃新闻反映称,三十年前,他在大连周水子机场被盖州市...
商务部:取消反制欧盟两家金融机... 中华人民共和国商务部令二〇二六年 第1号鉴于欧盟已取消对中国两家金融机构的制裁措施,现公布《关于取消...
过去24小时共有5艘船只通过霍... 总台记者当地时间24日获悉,过去24小时内,共有5艘船只通过霍尔木兹海峡,其中包括一艘伊朗油轮。(总...