文件
您可能想阅读MongoDB的基本内存问题,以及有关检查内存使用情况的简短讨论。
内存使用概述
该命令db.serverStatus()(docs)可以概述内存使用情况,尤其是:
> db.serverStatus().mem
{ "bits" : 64, "resident" : 27, "virtual" : 397, "supported" : true }
> db.serverStatus().tcmalloc
... not easy to read! ...
> db.serverStatus().tcmalloc.tcmalloc.formattedString
------------------------------------------------
MALLOC:        3416192 (    3.3 MiB) Bytes in use by application
MALLOC: +      4788224 (    4.6 MiB) Bytes in page heap freelist
MALLOC: +       366816 (    0.3 MiB) Bytes in central cache freelist
...
... a bunch of stats in an easier to read format ...
您的索引有多大?
db.stats() 可以显示所有索引的总大小,但是我们还可以使用来获取单个集合的详细信息 db.myCollection.stats()
例如,此命令将比较每个集合的索引大小:
> db.getCollectionNames().map(name => ({totalIndexSize: db.getCollection(name).stats().totalIndexSize, name: name})).sort((a, b) => a.totalIndexSize - b.totalIndexSize).forEach(printjson)
...
{ "totalIndexSize" : 696320, "name" : "smallCollection" }
{ "totalIndexSize" : 135536640, "name" : "bigCollection" }
{ "totalIndexSize" : 382681088, "name" : "hugeCollection" }
{ "totalIndexSize" : 511901696, "name" : "massiveCollection" }
现在,我们可以查看该大量集合的详细信息,以查看其哪些索引最昂贵:
> db.massiveCollection.stats().indexSizes
{
        "_id_" : 230862848,
        "groupId_1_userId_1" : 49971200,
        "createTime_1" : 180301824,
        "orderId_1" : 278528,
        "userId_1" : 50155520
}
这可以使我们更好地了解可能节省的地方。
(在这种情况下,我们有一个createTime相当大的索引-每个文档一个条目-并且我们决定没有它就可以生存。)