OverviewWhen you update a document in MongoDB, the update occurs in-place if the document has not grown in size. If the document did grow in size, however, then it might need to be relocated on disk to find a new disk location with enough contiguous space to fit the new larger document. This can lead to problems for write performance if the collection has many indexes since a move will require updating all the indexes for the document. Mongo adaptively learns if documents in a collection tend to grow, and if they do, it adds some padding so that the document has room to grow. This helps to prevent excessive movements on subsequent writes. This statistic is tracked separately for each collection. You can check the collection's current padding factor by running the collStats command helper in the shell. The padding factor indicates what the padding will be for new record allocations(for inserts, or on updates that grow a document and cause it to move to a new location).
> db.coll.stats()
{
"ns" : "...", ...,
"paddingFactor" : 1, ...,
"ok" : 1
}
As each document is written at a different point in time the padding for each document will not be the same. Also, as the padding factor is relative to the size of each document you cannot calculate the exact amount of padding for a collection based on the average document size and padding factor. The paddingfactor is 1.0 if there is no padding. 1.5 would indicate 50% padding on a new insert/moves. No padding after imports, repairs, compactions and initial replica syncsAfter compaction, repair and import operations, there is (generally) no padding as they were inserted and there were no updates (which would cause the paddingFactor to change). Thus you may see slower update performance after these cases, but the size required for storage will be lower. Shrinking DocumentsIf a document gets smaller (e.g., because of an $unset or $pop), the document does not move but stays at its current location. It thus effectively has more padding. Thus space is never reclaimed if documents shrink by large amounts and never grow again. To reclaim that space run a compact operation (or repair). Manual PaddingPadding in MongoDB is automatic. You should not have to do so manually. In exceptional cases one can do this though. The strategy is to add a faux field that assures allocation of a larger slot size for the document. The faux field is then removed; at this point there is extra room for expansion on a future update. Example below. > t = db.zcollection; > > db.setProfilingLevel(2); > > // b is a very long string > > t.insert({q:1}) > t.update({q:1},{$set:{yy:b}}) > > // note the "moved:true" in the output below -- indicating not enough > // padding to avoid moving a document. > db.system.profile.find() ... { "ts" : ISODate("2011-10-13T02:45:23.062Z"), "op" : "insert", "ns" : "test.zcollection", "millis" : 0, "client" : "127.0.0.1", "user" : "" } { "ts" : ISODate("2011-10-13T02:45:33.560Z"), "op" : "update", "ns" : "test.zcollection", "query" : { "q" : 1 }, "updateobj" : { "$set" : { "yy" : "aaaaaaaaa..." } }, "nscanned" : 1, "moved" : true, "millis" : 3, "client" : "127.0.0.1", "user" : "" } > > // not important what value of 'padding' is, only its length: > t.insert({q:2,padding:ourpaddingstring}) > t.update({q:2},{$unset:{padding:1}}) > t.update({q:2},{$set:{yy:b}}) > > // no "moved:true" below (which is good). Note however that the automatic adjustment > // of a collection's padding factor normally achieves this regardless. Manually padding > // is rarely necessary. > db.system.profile.find() ... { "ts" : ISODate("2011-10-13T02:46:34.920Z"), "op" : "insert", "ns" : "test.zcollection", "millis" : 1, "client" : "127.0.0.1", "user" : "" } { "ts" : ISODate("2011-10-13T02:46:42.775Z"), "op" : "update", "ns" : "test.zcollection", "query" : { "q" : 2 }, "updateobj" : { "$unset" : { "padding" : 1 } }, "nscanned" : 1, "millis" : 2, "client" : "127.0.0.1", "user" : "" } { "ts" : ISODate("2011-10-13T02:46:55.831Z"), "op" : "update", "ns" : "test.zcollection", "query" : { "q" : 2 }, "updateobj" : { "$set" : { "yy" : "aaaaaaaaa..." } }, "nscanned" : 1, "millis" : 2, "client" : "127.0.0.1", "user" : "" } See Also |

PLEASE POST QUESTIONS IN THE USER GROUPS FORUM. Post non-question comments and helpful hints here.
blog comments powered by Disqus