设为首页 收藏本站
查看: 1090|回复: 0

[经验分享] 深入MongoDB内存溢出调优

[复制链接]

尚未签到

发表于 2018-10-25 12:20:11 | 显示全部楼层 |阅读模式
  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: })


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-626349-1-1.html 上篇帖子: CentOS6.5上源码安装MongoDB3.2.1-xiaoxiaot 下篇帖子: Mongodb被动(passive)节点配置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表