1 问题
mongodb中某collection的记录数达到几千万级别的时候,mongodb占据的内存达到几十G,
节点内存耗尽,宕机
2 分析
2.1 问题定位
登陆mongodb,查看mongodb的服务状态
db.stats()
结果如下:
collections: 10
objects: 22591647
avgObjSize: 1204左右
dataSize: 27205964928
storageSize: 5663232000
indexes: 39
indexSize: 14344634368
ok: 1
2.2 mongodb索引
mongodb会将最近常用的值保存在RAM中,就有可能导致占用的内存变大。
官网描述如下:
Indexes that Hold Only Recent Values in RAM
Indexes do not have to fit entirely into RAM in all cases. If the value of the indexed field increments with every insert, and most queries select recently added documents; then MongoDB only needs to keep the parts of the index that hold the most recent or “right-most” values in RAM. This allows for efficient index use for read and write operations and minimize the amount of RAM required to support the index.
参考: