How to find an index in RAM or Disk [MongoDB]?

What is the best way to determine if an index is in RAM or disk in mongodb?

+6
source share
2 answers

On the one hand, it is possible that only some parts of the index are in RAM http://docs.mongodb.org/manual/applications/indexes/#indexing-right-handed

Indexes do not have to fully fit into RAM in all cases. If the value of the indexed field grows with each insertion, and most queries select recently added documents; then MongoDB needs to save only those parts of the index that contain the most recent or "rightmost" values ​​in RAM. This allows you to efficiently use the index for read and write operations and minimize the amount of RAM needed to support the index.

On the other hand, db.serverStatus() gives you the totality of the information you need. Check out http://docs.mongodb.org/manual/reference/server-status/#server-status-indexcounters :

The value of indexCounters.btree.hits reflects the number of hits on the index, and mongod can return the index from memory.

+4
source

Yes, there is a way. You can use totalIndexSize() or stats() to check the size of the index

 > db.collection.totalIndexSize() > 1073741824 > db.collection.stats() > { "ns" : "collection.test", "count" : 100006, "size" : 72104, "avgObjSize" : 4506.5, "storageSize" : 688128, "numExtents" : 3, "nindexes" : 1, "lastExtentSize" : 524288, "paddingFactor" : 1.0170000000000319, "systemFlags" : 1, "userFlags" : 0, "totalIndexSize" : 1073741824, "indexSizes" : { "_id_" : 1073741824 }, "ok" : 1 

Both return the size of the collection index in bytes. In the above example, the index size is 1GB .

UPDATE

To find the total index size of the entire database, you can use stats()

  > db.stats() { "db" : "test", "collections" : 10, "objects" : 5909990, "avgObjSize" : 2888.31186440677965, "dataSize" : 170123304, "storageSize" : 14745603499, "numExtents" : 17, "indexes" : 8, "indexSize" : 1073741824, "fileSize" : 50331648, "nsSizeMB" : 163453, "ok" : 1 } 

Now you can compare indexSize with the RAM of the machine to check if the index is suitable in memory or not. And you can increase the size of RAM to accommodate the full index.

Hope this helps

+1
source

All Articles